This SDK was created to enable rapid efiicient development using Shopify's API.
Easily install this package with composer
composer require robby-bugatti/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
When the SDK is run, it defaults to running in a strict environment. This requires that: A) The HMAC Hash of request parameters matches output generated by your application's secret key B) Authentication responses contain a 'state' parameter that matches the one placed in the request
Because you can be deploying your application in distributed environments, or be using any number of storage engines, this SDK will not store these state variables for you. However, it does expose a few functions for generating and managing them
\Shopify\Auth::generateNonce();
// Returns a hashed string of <store>.<timestamp>, using API Secret as key
This will return a hashed string, composed by concatenating the store name with a timestamp, using the API Secret as the key.
\Shopify\Auth::setNonce( $nonce = NULL )
This will set a nonce in the Auth Object. It will be added to the authorizationUrl, and when required, compare it to the ?state=<nonce_here> returned by Shopify
\Shopify\Auth::checkNonce( $nonce = NULL )
This will return TRUE or FALSE, depending on if the nonce in the URL matches a nonce set through setNonce() This function is automatically run during accessToken() in strict environments, so you shouldnt need to call it explicitly
For full example, check out Full Auth Example with Comments
To use the SDK for OAuth purposes, you need to provide your api_key, api_secret, permissions, and redirect_uri
// Set our options for initializing the SDK
$options = array(
'api_key' => 'some_random_api_key',
'api_secret' => 'some_random_api_secret',
'redirect_uri' => "http://your_app.com/redirect_uri",
'permissions' => "<permissions your applicaiton requires, comma separated>",
'store' => "myshopify.domain.com"
);
\Shopify\Shopify::init($options);
if(!isset($_GET['code']))
{
$nonce = \Shopify\Auth::generateNonce();
// Store this somewhere so we can compare it later
$storageEngine->store($nonce);
// Redirect to Shopify to start OAuth
header("Location: ".\Shopify\Auth::authorizationUrl());
} else {
$nonce = $storageEngine->retrieve($nonce);
\Shopify\Auth::setNonce($nonce);
// We can go ahead and get the access token
echo \Shopify\Auth::accessToken();
// This should return something that looks like this:
// 53e20e750c89274d02b53927135fd664
}
Now that we have an access token, we can make authenticated requests to Shopify. Once you have an Access Token, you only need to provide the token and the store it belongs to
$options = array(
'access_token' => '53e20e750c89274d02b53927135fd664',
'store' => 'myshopify.domain.com'
);
\Shopify\Shopify::init($options);
You now have access to all the methods the SDK provides!
The SDK uses static methods to fetch data from Shopify
// Basic layout
\Shopify\<requested_object>::{method}($params);
// Fetch shop info
\Shopify\Shop::get(); // This doesn't require params, because theres only one store object
// Get all products
\Shopify\Product::all(); // Returns array of Product objects
\Shopify\Order::get(<order_id>); // Return a single order
To create objects in the Shopify domain, simply set all the attributes you want to put on the object, and call save();
$product = new \Shopify\Product([
'title' => 'Random title',
'handle' => "Some Product",
'product_type' => "Application",
]);
$product->create();
echo $product->id;
// 2178508200
To update an object through the SDK, just call update() on an instantiated object
// Fetch and update inline
$product = \Shopify\Product::get(2178508200);
$product->title = "A new product title";
$product->update();
// If you know the ID, you can create a *new* object and just call update
$opts = array(
'id' => 2178508200,
'title' => "A new product title"
);
$product = new \Shopify\Product($opts);
$product->update();
To delete an object, simply call the objects delete() method, passing the ID
(new \Shopify\Product(123412341))->delete();
// returns NULL
The SDK is designed to throw Exceptions when an error is encountered. Wrap calls to Shopify in try / catch statements, and use your desired exception handler.
try {
$product = new \Shopify\Product(array());
} catch (Exception\CurlException $e) {
echo $e->getMessage();
} catch (Exception\ApiException) {
echo $e->getMessage();
// "Title cannot be blank"
}
\\ Exception\CurlException => cURL failed to connect
\\ Exception\ApiException => There was an API error. [Invalid POST data, Invalid Endpoint, etc.]