Giter Site home page Giter Site logo

shopify-php-sdk's Introduction

Build Status

Shopify PHP SDK

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

Installation

Easily install this package with composer

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:

$client = new Shopify\Api(array(
    'api_key' => '<api_key>',
    'api_secret' => '<api_secret>',
    'myshopify_domain' => 'store.myshopify.com',
    'access_token' => '<store_access_token>'
));

If you are using a Private App for use on an individual store:

$client = new Shopify\PrivateApi(array(
    'api_key' => '<api-key>',
    'password' => '<password>',
    'shared_secret' => '<shared-secret>',
    'myshopify_domain' => '<store>.myshopify.com'
));

Once the client is initialized, you can then create a service, and use it to communicate with the api

Reading

$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

$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

$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

$service = new Shopify\Service\ProductService($client);
$service->delete($productId);

GraphQL

Query

$service = new Shopify\Service\GraphQLService($client);
$service->graph(
  '{
    products(query: "created_at:<2019", first: 5) {
      edges {
        node {
          title
          description
        }
      }
    }
  }'
);

Mutation

$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

$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

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

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

Shopify API Reference

shopify-php-sdk's People

Contributors

robwittman avatar busfox avatar mihnsen avatar rkazaniszyn avatar

Watchers

James Cloos 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.