2017 © Pedro Peláez
 

project shopify-api

Shopify API for PHP

image

jivochat/shopify-api

Shopify API for PHP

  • Monday, May 28, 2018
  • by yurka44
  • Repository
  • 1 Watchers
  • 0 Stars
  • 460 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 4 Forks
  • 0 Open issues
  • 43 Versions
  • 5011 % Grown

The README.md

Shopify API (Deprecated, uses Guzzle 3.9)

Please note: the new version using Guzzle 6.2 is actively maintained here, (*1)

An object-oriented approach towards using the Shopify API., (*2)

Supported Objects / Endpoints:

Composer

$ composer require janicheg/shopify-api v0.9.9.*

Usage without Laravel

// Assumes setup of client with access token.
$mgr = ShopifyApi\Manager::init($shop, $token);
$mgr->getProduct($product_id = 123);              // returns ShopifyApi/Models/Product

// Alternatively, we may call methods on the API object.
$mgr->api('products')->show($product_id = 123);   // returns array

See Facade usages for other methods available.

Usage with Laravel

In your config/app.php, (*3)

Add the following to your providers array:

ShopifyApi\Providers\ShopifyServiceProvider::class,

Add the following to your aliases array:

'Shopify' => ShopifyApi\Support\ShopifyFacade::class,

Replace following variables in your .env

SHOPIFY_DOMAIN=your-shop-name.myshopify.com
SHOPIFY_TOKEN=your-token-here

Using the Facade gives you Manager

Methods called on Manager will cascade down onto Client via the __call method., (*4)

If you're using Laravel, Models will return \Illuminate\Support\Collection instead of array., (*5)

Shopify::getProduct($product_id = 123);     // returns ShopifyApi/Models/Product
Shopify::getAllProducts();                  // returns Collection|array of ShopifyApi/Models/Product

Shopify::getVariant($variant_id = 456);     // returns ShopifyApi/Models/Variant
Shopify::getAllVariants($product_id = 123); // returns Collection|array of ShopifyApi/Models/Variant

Shopify::getOrder($order_id = 789);         // returns ShopifyApi/Models/Order
Shopify::getAllOrders();                    // returns a Collection|array of ShopifyApi/Models/Order

Shopify::getMetafield($metafield_id = 123); // returns ShopifyApi/Models/Metafield

Shopify::getDiscount($dicount_id = 123);    // returns ShopifyApi/Models/Discount
Shopify::getAllDiscounts();                 // returns Collection|array of ShopifyApi/Models/Discount

Shopify::getAllWebhooks();                  // returns Collection|array of ShopifyApi/Models/Webhook

// Alternatively, we may call methods on the API object.

Shopify::api('products')->show($product_id = 123);           // returns array
Shopify::api('products')->all();                             // returns array
Shopify::api('products')->count();                           // returns int

Shopify::api('variants')->show($variant_id = 456);           // returns array
Shopify::api('variants')->product($product_id = 123)->all(); // returns array

Shopify::api('orders')->show($order_id = 123);               // returns array
Shopify::api('orders')->all();                               // returns array
Shopify::api('orders')->count();                             // returns int

Shopify::api('custom_collections')->show($cc_id = 123);      // returns array
Shopify::api('custom_collections')->all();                   // returns array
Shopify::api('custom_collections')->count();                 // returns int

Shopify::api('discounts')->show($discount_id = 123);         // returns array
Shopify::api('discounts')->all();                            // returns array               
Shopify::api('discounts')->count();                          // returns int

Shopify::api('webhooks')->show($webhook_id = 123);           // returns array
Shopify::api('webhooks')->all();                             // returns array
Shopify::api('webhooks')->count();                           // returns int

Examples of saving data.

Creating a product using a model
$product = Shopify::getProduct();
$product->setTitle('Burton Custom Freestyle 151');
$product->setBodyHtml('<strong>Good snowboard!<\/strong>');
$product->setVendor('Burton');
$product->setProductType('Snowboard');
$product->setTags(['Barnes & Noble', 'John\'s Fav', '"Big Air"']);
$product->save();
Updating a product using a model
$product = Shopify::getProduct(123);
$product->setTitle('Burton Freestyle 152');
$product->save();
Add a product to a collection
$collection = Shopify::getCustomCollection(123);
$collection->add(456);

or, (*6)

$collection = Shopify::getCustomCollection(123);
$collection->add([456,789]);
Creating and updating metafields for resources is more intuitive using key / namespace.
// The 'value_type' property will be determined automatically if omitted
$product->createMetafield('in_stock', 'inventory', ['value' => 123]); 

$product->updateMetafield('in_stock', 'inventory', ['value' => 122]);

$product->updateOrCreateMetafield('in_stock', 'inventory', ['value' => 200]);

// Support is included for arrays and objects (json encodable) and null
$product->createMetafield('board_specs', 'criteria', ['value' => new MyJsonSerializableObject()]);

Embedded Apps

Get a token for a redirect response.

Shopify::getAppInstallResponse(
    'your_app_client_id', 
    'your_app_client_secret',
    'shop_from_request',
    'code_from_request'
);

// returns (object) ['access_token' => '...', 'scopes' => '...']

Verify App Hmac (works for callback or redirect)

ShopifyApi\Util::validAppHmac(
    'hmac_from_request', 
    'your_app_client_secret', 
    ['shop' => '...', 'timestamp' => '...', ...]
);

Verify App Webhook Hmac

ShopifyApi\Util::validWebhookHmac(
    'hmac_from_request', 
    'your_app_client_secret', 
    file_get_contents('php://input')
);

Caching

If you want to sprinkle a little caching in, setup a service provider and extend the \ShopifyApi\Providers\ShopifyServiceProvider., (*7)

Here is an example Provider:

<?php

namespace App\Providers;

use App;
use ShopifyApi\Manager;
use ShopifyApi\Models\Product;
use ShopifyApi\Providers\ShopifyServiceProvider as BaseServiceProvider;

/**
 * Class ShopifyServiceProvider
 */
class ShopifyServiceProvider extends BaseServiceProvider
{
    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        parent::register();

        /** @var Manager $shopify */
        $shopify = app('shopify');

        $shopify->setApiCache(Product::class, function($client, $params = null) {

            // No caching for collections.
            if (is_array($params)) {
                // Returning falsy will result in the default api behavior.
                return null;
            }

            $key = "shopify_product_".((string) $params);

            // Assuming you Cache::put($key, $product->getData()); elsewhere
            // If the cache is empty, the resource will be fetched from the api
            // as normal.
            $data = Cache::get($key);

            return $data ? new Product($client, $data) : null;
        });
    }
}

Contributors

Special Thanks

This repository's structure was modeled after the robust cdaguerre/php-trello-api., (*8)

Todo

  • Migrate from guzzle/guzzle to guzzlehttp/guzzle, bump version.
  • Publish files for Laravel setup
  • Artisan Command to create token

License

MIT., (*9)

The Versions

28/05 2018

dev-master

9999999-dev

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

28/05 2018

1.2.1

1.2.1.0

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

27/02 2018

1.2

1.2.0.0

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

27/02 2018

1.1

1.1.0.0

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

31/10 2017

1.0

1.0.0.0

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

11/07 2017

v0.9.9.9

0.9.9.9

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

22/06 2017

v0.9.9.8

0.9.9.8

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

05/06 2017

v0.9.9.7

0.9.9.7

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

31/05 2017

v0.9.9.6

0.9.9.6

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

04/05 2017

v0.9.9.5

0.9.9.5

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

02/05 2017

v0.9.9.4

0.9.9.4

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

02/05 2017

v0.9.9.3

0.9.9.3

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

01/05 2017

v0.9.9.2

0.9.9.2

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

27/04 2017

v0.9.9.1

0.9.9.1

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

27/04 2017

v0.9.9.0

0.9.9.0

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

18/04 2017

v0.9.7.12

0.9.7.12

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

14/04 2017

v0.9.8.0

0.9.8.0

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

13/04 2017

v0.9.7.11

0.9.7.11

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

12/04 2017

v0.9.7.10

0.9.7.10

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

10/04 2017

v0.9.7.9

0.9.7.9

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

10/04 2017

v0.9.7.8

0.9.7.8

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

10/04 2017

v0.9.7.7

0.9.7.7

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

21/03 2017

v0.9.7.6

0.9.7.6

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

14/03 2017

v0.9.7.5

0.9.7.5

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

12/03 2017

v0.9.7.4

0.9.7.4

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

23/01 2017

v0.9.7.3

0.9.7.3

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

05/01 2017

v0.9.7.2

0.9.7.2

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

03/01 2017

v0.9.7.1

0.9.7.1

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

02/11 2016

v0.9.7

0.9.7.0

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

27/10 2016

v0.9.6

0.9.6.0

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

27/10 2016

v0.9.5

0.9.5.0

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

27/10 2016

v0.9.4

0.9.4.0

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

26/10 2016

v0.9.2

0.9.2.0

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

07/10 2016

v0.9.1

0.9.1.0

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

07/10 2016

v0.9

0.9.0.0

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

06/10 2016

v0.8

0.8.0.0

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

05/10 2016

v0.7

0.7.0.0

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

05/10 2016

v0.6

0.6.0.0

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

23/08 2016

v0.0.5

0.0.5.0

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

23/08 2016

v0.0.4

0.0.4.0

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

23/08 2016

v0.0.3

0.0.3.0

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

16/08 2016

v0.0.2

0.0.2.0

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks

16/08 2016

v0.0.1

0.0.1.0

Shopify API for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api shopify webhooks