dan/shopify
Composer 安装命令:
composer create-project dan/shopify
包简介
Shopify API with Laravel integrations using latest Guzzle.
README 文档
README
A fluent and object-oriented approach for using the Shopify API.
Supported Objects / Endpoints:
- Asset
- Customer
- Dispute
- Fulfillment
- FulfillmentService
- Image
- Metafield
- Order
- Product
- Risk
- Shop
- Theme
- Variant
- Webhook
Versions
| PHP | Package |
|---|---|
| 8 | 5.0.* |
| 7 | 4.0.* |
| YOLO | dev-master |
Composer
composer require dan/shopify
Basic Usage
The APIs all function alike, here is an example of usage of the products API.
$api = Dan\Shopify\Shopify::make($shop = 'shop-name.myshopify.com', $token = 'shpua_abc123'); // Shop information $api->shop(); // array dictionary // List of products $api->products->get(); // array of array dictionaries // Attach query parameters to a get request $api->products->get(['created_at_min' => '2023-03-25']); // array of array dictionaries // A specific product $api->products('123456789')->get(); // array dictionary // Get all variants for a product $api->products('123456789')->variants->get(); // array of array dictionaries // Get a specific variant for a specific product $s->api2()->products('123456789')->variants('567891234')->get(); // array dictionary // Append URI string to a get request $api->orders('123456789')->get([], 'risks'); // array dictionary // Create a product. // See https://shopify.dev/docs/api/admin-rest/2023-01/resources/product#post-products $api->products->post(['title' => 'Simple Test']); // array dictionary // Update something specific on a product $api->products('123456789')->put(['title' => 'My title changed.']); // array dictionary
Basic (very basic) GraphQL
The collection and model utilities that are available ->find(...) and ->findMany(...) for RESTful endpoints are NOT available for GraphQL.
Some endpoints are only available through Shopify's GraphQL library. This makes me sad because GraphQL is not as readable or intuitive as RESTful APIs, less people understand it, and it's harder to train people on. That said, if you want to jam out with your graphql, there is a client method to assist you.
For example, fetch delivery profiles (only available in GraphQL).
Note: You can safely use the
graphql(...)helper method without any concern of changing the state on theDan\Shopify\Shopify::class.
$query = "{ deliveryProfiles (first: 3) { edges { node { id, name, } } } }" $api->graphql($query); // hipster
Using cursors
Shopify doesn't jam with regular old pagination, sigh ...
As of the 2019-10 API version, Shopify has removed per page pagination on their busiest endpoints.
With the deprecation of the per page pagination comes a new cursor based pagination.
You can use the next method to get paged responses.
Example usage:
// First call to next can have all the usual query params you might want. $api->orders->next(['limit' => 100, 'status' => 'closed'); // Further calls will have all query params preset except for limit. $api->orders->next(['limit' => 100]);
Metafields!
There are multiple endpoints in the Shopify API that have support for metafields.
In effort to support them all, this API has been updated to allow chaining ->metafields from any endpoint.
This won't always work as not every endpoint supports metafields, and any endpoint that doesn't support metafields will result in a 404.
Below are examples of all the endpoints that support metafields.
// Get our API $api = Dan\Shopify\Shopify::make($shop, $token); // Store metafields $api->metafields->get(); // Metafields on an Order $api->orders($order_id)->metafields->get(); // Metafields on a Product $api->products($product_id)->metafields->get(); // Metafields on a Variant $api->products($product_id)->variants($variant_id)->metafields->get(); // Metafields on a Customer $api->customers($customer_id)->metafields->get(); // Metafields can also be updated like all other endpoints $api->products($product_id)->metafields($metafield_id)->put($data);
Usage with Laravel
Single Store App
In your config/app.php
Add the following to your providers array:
Requires for private app (env token) for single store usage of oauth (multiple stores)
Dan\Shopify\Integrations\Laravel\ShopifyServiceProvider::class,
Add the following to your aliases array:
If your app only interacts with a single store, there is a Facade that may come in handy.
'Shopify' => Dan\Shopify\Integrations\Laravel\ShopifyFacade::class,
For facade usage, replace the following variables in your .env
SHOPIFY_DOMAIN=your-shop-name.myshopify.com SHOPIFY_TOKEN=your-token-here
Optionally replace following variables in your .env
Empty or admin defaults to oldest supported API, learn more
SHOPIFY_API_BASE="admin/api/2022-07"
Using the Facade gives you Dan\Shopify\Shopify
It will be instantiated with your shop and token you set up in
config/shopify.php
Review the Basic Usage above, using the Facade is more or less the same, except you're only interacting with the one store in your config.
// Facade same as $api->shop(), but for just the one store. Shopify::shop(); // Facade same as $api->products->get(), but for just the one store. Shopify::products()->get(); // Facade same as $api->products('123456789')->get(), but for just the one store. Shopify::products('123456789')->get();
Oauth Apps
Making a public app using oauth, follow the Shopify docs to make your auth url, and use the following helper to retrieve your access token using the code from your callback.
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)
Dan\Shopify\Util::validAppHmac( 'hmac_from_request', 'your_app_client_secret', ['shop' => '...', 'timestamp' => '...', ...] );
Verify App Webhook Hmac
Dan\Shopify\Util::validWebhookHmac( 'hmac_from_request', 'your_app_client_secret', file_get_contents('php://input') );
Contributors
Todo
- Artisan Command to create token
License
MIT.
dan/shopify 适用场景与选型建议
dan/shopify 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 25.6k 次下载、GitHub Stars 达 23, 最近一次更新时间为 2017 年 07 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「shopify」 「laravel」 「webhooks」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dan/shopify 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dan/shopify 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dan/shopify 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PSR-7 compatible library for making CRUD API endpoints
Shopify package for Laravel to aide in app development
A basic Shopify API wrapper with REST and GraphQL support.
A set of APIs to retrieve data from Shopify
Alfabank REST API integration
PHP SDK for Shopify API
统计信息
- 总下载量: 25.6k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 23
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-07-19