https://github.com/robwittman/shopify-php-sdk
PHP SDK for development with the Shopify API
https://github.com/robwittman/shopify-php-sdk
ecommerce php php-sdk shopify shopify-api
Last synced: 12 months ago
JSON representation
PHP SDK for development with the Shopify API
- Host: GitHub
- URL: https://github.com/robwittman/shopify-php-sdk
- Owner: robwittman
- License: mit
- Created: 2016-04-17T16:32:16.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2024-06-13T12:52:36.000Z (almost 2 years ago)
- Last Synced: 2025-03-30T00:07:23.633Z (about 1 year ago)
- Topics: ecommerce, php, php-sdk, shopify, shopify-api
- Language: PHP
- Size: 472 KB
- Stars: 67
- Watchers: 6
- Forks: 40
- Open Issues: 22
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/robwittman/shopify-php-sdk)
# Shopify PHP SDK
This SDK was created to enable rapid efficient development using Shopify's API.
## Installation
Easily install this package with composer
```shell
composer require robwittman/shopify-php-sdk
```
Before you can start using this SDK, you have to create a Shopify Application
You can now use the API key and secret to generate access tokens, which can then access a stores data
## Initialization
To initialize the Api Client:
```php
$client = new Shopify\Api(array(
'api_key' => '',
'api_secret' => '',
'myshopify_domain' => 'store.myshopify.com',
'access_token' => ''
));
```
If you are using a Private App for use on an individual store:
```php
$client = new Shopify\PrivateApi(array(
'api_key' => '',
'password' => '',
'shared_secret' => '',
'myshopify_domain' => '.myshopify.com'
));
```
Once the client is initialized, you can then create a service, and use it to communicate with the api
### Reading
```php
$service = new Shopify\Service\ProductService($client);
$service->all(); #Fetch all products, with optional params
$service->get($productId); # Get a single product
$service->count(); # Count the resources
```
### Creating
```php
$service = new Shopify\Service\ProductService($client);
$product = new Shopify\Object\Product();
# Set some product fields
$product->title = 'Test Product';
$product->vendor = 'Printer';
$service->create($product);
```
### Updating
```php
$service = new Shopify\Service\ProductService($client);
$product = $service->get($productId);
# Set some product fields
$product->title = 'Test Product';
$product->vendor = 'Printer';
$service->update($product);
```
### Deleting
```php
$service = new Shopify\Service\ProductService($client);
$service->delete($productId);
```
### GraphQL
#### Query
```php
$service = new Shopify\Service\GraphQLService($client);
$service->graph(
'{
products(query: "created_at:<2019", first: 5) {
edges {
node {
title
description
}
}
}
}'
);
```
#### Mutation
```php
$service = new Shopify\Service\GraphQLService($client);
$service->graph(
'mutation productCreate($input: ProductInput!){
productCreate(input: $input) {
product {
id
}
}
}',
['input' => ['title' => 'Sweet new product','productType' => 'Snowboard','vendor' => 'JadedPixel']]
);
```
## Authentication
Authentication to Shopify's API is done through access tokens, which are obtained through OAuth. To get a
token, there is a helper library packaged with this client
```php
$client = new Shopify\Api($params);
$helper = $client->getOAuthHelper();
$redirectUri = 'https://localhost/install.php';
$scopes = 'write_products,read_orders,...';
$authorizationUrl = $helper->getAuthorizationUrl($redirectUri, $scopes);
header("Location: {$authorizationUrl}");
```
At your `redirect_uri`, instantiate the helper again to get an access token
```php
$client = new Shopify\Api($params);
$helper = $client->getOAuthHelper();
$token = $helper->getAccessToken($code);
echo $token->access_token;
echo $token->scopes;
```
By default, this uses simple session storage. You can implement a custom class that implements `PersistentStorageInterface`,
pass that to `new Shopify\Api()`, and `OAuthHelper` will use that instead. This will be required if authorization requests and
redirects may be directed to different servers.
### Using objects
Object properties can be accessed using `object->property`. Nested objects are instantiated classes. All timestamp fields are instances of `\DateTime`.
```php
use Shopify\Enum\Fields\ProductFields;
use Shopify\Enum\Fields\ProductVariantFields;
$product = $service->get($productId);
echo $product->created_at->format('Y-m-d H:i:s');
echo $product->title;
foreach ($product->variants as $variant) {
echo $variant->option1;
echo $variant->option2;
}
```
## References
[Shopify Partner Login](https://partners.shopify.com)
[Shopify API Reference](https://help.shopify.com/api/reference)