cian / shopify
简单的PHP、Laravel Shopify API包。
Requires
- php: >=7.0
- guzzlehttp/guzzle: >=7.0
- illuminate/support: ^5|^6|^7|^8
Requires (Dev)
- mockery/mockery: ^1
- phpunit/phpunit: ^8.5
- symfony/var-dumper: ^5.0
This package is auto-updated.
Last update: 2024-09-23 08:41:25 UTC
README
简单的PHP、Laravel Shopify API包。
安装
要求
- PHP 7.0+
- Laravel ^5|^6|^7|^8
composer require cian/shopify
Laravel
php artisan vendor:publish --provider="Cian\Shopify\ShopifyServiceProvider"
如果你的Laravel版本 <= 5.4,别忘了添加服务提供者和别名。
// <Root>/config/app.php [ "providers" => [ // other providers ... Cian\Shopify\ShopifyServiceProvider::class ], "aliases" => [ // other aliases ... 'Shopify' => \Cian\Shopify\ShopifyFacade::class, 'ShopifyMacro' => \Cian\Shopify\ShopifyMacroFacade::class ] ]
使用方法
你需要查看 Shopify API文档 以获取更多关于每个API选项的知识。
以下是一个示例,演示如何在Laravel中使用此包。
<?php namespace App\Services; use Cian\Shopify\Shopify; class GetShopifyOrdersService { protected $shopify; public function __construct(Shopify $shopify) { $this->shopify = $shopify; } public function exec() { $response = $this->shopify ->setWebsite('mystore') ->getOrders([ 'limit' => 100, // more options of get orders api ... ]); // always get response body using this way. $orders = $response->getBody(); return $orders; } }
响应
你将获得每个API调用实例的 \Cian\Shopify\Response。
此对象为你提供简单的接口来访问 Shopfiy分页Header。
namespace App; // use laravel facade. use Shopify; $response = Shopify::setWebsite('mystore')->getOrders([/** options */]); $response->hasNextPage(); // boolean $response->getNextLink(); // null or next page api url string. $response->hasPreviousPage(); // boolean $response->getPreviousLink(); // null or previous page api url string. $response->isLastPage(); // boolean /** * This method get content from guzzle response. * and run json_decode before return. * you can pass json_decode options via this method, * here just show you the default values, all options are optional! */ $response->getBody(true, 512, 0); /** * If you prefer handle response by your self. * you can get original response like below. */ $guzzleResponse = $response->getOriginalResponse();
Laravel中间件
将 \Cian\Shopify\Http\Middleware\VerifyWebHookMiddleware.php 添加到中间件列表中,就像其他中间件一样。
请确保你的 .env 文件包含 SHOPIFY_MYSTORE_SECRET 字段。
如果你有多个商店,只需确保每个网站在 config/shopify.php 中都有 secret。
配置
api_presets
我们可以在特定API中提供一些通用内容。
例如
如果你的应用程序只关心订单的id、line_items和billing_address,可以这样设置
然后你可以创建一个如下预设
// config/shopify.php return [ // othere properties... 'api_presets' => [ // the key(my_order_common) can be any string, just don't duplicate. 'my_order_common' => [ 'fields' => [ 'id', 'line_items', 'billing_address' ] ] ] ];
在你的代码中
<?php namespace App; use Cian\Cian\Shopify; $shopify = new Shopify($guzzleClient, $config); $keep = false; // keep using this preset for each call or not, default is false. $response = $shopify ->setWebsite('mystore') ->setApiPreset('my_order_common', $keep) ->getOrders([/** more options */]);
当你调用 setApiPreset 并同时提供选项给API时,
它们将被合并,你的上下文选项将被尊重。
你可能不知道Shopify API有一个已知的bug,它可能会给我们缓存(过期)的数据。
Shopify CS告诉我们,我们可以使用 fields 参数来强制获取新鲜数据。
这个功能也适用于这个问题。🤘
ShopifyMacro
这意味着你想要轻松获取数据,而不关心性能。
例如,你想要获取 getOrders API的所有结果,你不想处理分页。
那么你可以使用这个类。🍻🍻🍻
注意:当你获取大量数据时可能会遇到内存问题。
示例
以下示例中我们使用了依赖注入,但门面(facade)也是可用的。
namespace App\Services; use Cian\Shopify\ShopifyMacro; class MyService { protected $shopifyMacro; public function __construct(ShopifyMacro $shopifyMacro) { $this->shopifyMacro = $shopifyMacro; } public function exec() { $options = [ 'limit' => 250, // get 250 records per request. 'created_at_min' => '2020-04-08T12:00:00+00:00' // set min date // other getOrders options .. ]; // You will get response body instead of \Cian\Shopify\Response instance. $orders = $this->shopifyMacro ->setWebsite('mystore') /** * setFormatter is optional! * it can let getters return specific format. * this may decline some memory usage. */ ->setFormatter(function ($order) { return [ 'id' => $order['id'] ]; }) ->getOrders($options); // do something with orders ... } }