Giter Club home page Giter Club logo

shopify-php-sdk's Introduction

Shopify PHP SDK

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

This SDK was created to enable rapid efiicient development using Shopify's API.

Installation

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

A Note on Strict Execution

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

Authentication

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!

Reading

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

Creating

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

Updating

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();

Deleting

To delete an object, simply call the objects delete() method, passing the ID

(new \Shopify\Product(123412341))->delete();
// returns NULL

Error Handling

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.]

References

Shopify Partner Login

Shopify API Reference

SDK Usage Examples

shopify-php-sdk's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.