janicheg/shopify-api

Shopify API for PHP

安装量: 2,646

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 6

类型:项目

1.2 2018-02-27 13:39 UTC

README

请注意:使用 Guzzle 6.2 的 新版本 在这里 积极维护

面向 Shopify API 的面向对象方法。

支持的对象/端点

Composer

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

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.

Laravel 中的使用

在你的 config/app.php

将以下内容添加到你的 providers 数组中

ShopifyApi\Providers\ShopifyServiceProvider::class,

将以下内容添加到你的 aliases 数组中

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

替换 .env 中的以下变量

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

使用外观可以给你 Manager

Manager 上调用的方法将通过 __call 方法级联到 Client

如果你使用 Laravel,Models 将返回 \Illuminate\Support\Collection 而不是 array

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

保存数据的示例。

使用模型创建产品
$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();
使用模型更新产品
$product = Shopify::getProduct(123);
$product->setTitle('Burton Freestyle 152');
$product->save();
将产品添加到收藏夹
$collection = Shopify::getCustomCollection(123);
$collection->add(456);

$collection = Shopify::getCustomCollection(123);
$collection->add([456,789]);
使用键/命名空间创建和更新资源元字段更为直观。
// 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()]);

嵌入式应用程序

获取重定向响应的令牌。

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

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

验证应用程序 Hmac (适用于回调或重定向)

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

验证应用程序 Webhook Hmac

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

缓存

如果你想在其中加入一些缓存,请设置服务提供商并扩展 \ShopifyApi\Providers\ShopifyServiceProvider

以下是一个示例提供商

<?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;
        });
    }
}

贡献者

特别感谢

此存储库的结构是基于健壮的 cdaguerre/php-trello-api 模式。

待办事项

  • guzzle/guzzle 迁移到 guzzlehttp/guzzle,增加版本号。
  • 发布用于 Laravel 设置的文件
  • Artisan 命令以创建令牌

许可证

MIT。