dan/shopify

使用最新 Guzzle 集成 Shopify API 的 Laravel。

安装次数: 24,228

依赖: 0

建议者: 0

安全: 0

星级: 24

关注者: 7

分支: 9

开放问题: 1

类型:项目


README

使用 Shopify API 的流畅且面向对象的编程方式。

支持的对象/端点

版本

Composer

composer require dan/shopify

基本用法

所有 API 都具有类似的功能,以下是一个使用产品 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

基本(非常基本的)GraphQL

对于 RESTful 端点,可用的集合和模型工具 ->find(...)->findMany(...) 都不可用于 GraphQL。

某些端点仅通过 Shopify 的 GraphQL 库可用。这让我很沮丧,因为 GraphQL 不如 RESTful API 可读或直观,了解它的人较少,而且培训起来更困难。话虽如此,如果你想用 graphql 进行创作,有一个客户端方法可以协助你。

例如,获取配送配置文件(仅在 GraphQL 中可用)。

注意:您可以使用 graphql(...) 辅助方法,而无需担心更改 Dan\Shopify\Shopify::class 的状态。

$query = "{
  deliveryProfiles (first: 3) {
    edges {
      node {
        id,
        name,
      }
    }
  }
}"

$api->graphql($query); // hipster

使用游标

Shopify 不支持常规的分页, sigh ...

截至 2019-10 API 版本,Shopify 已在其最繁忙的端点上删除了每页分页。
随着每页分页的弃用,出现了一种新的基于游标的分页。
您可以使用 next 方法获取分页响应。
示例用法

// 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]);

元字段!

Shopify API 中有多个端点支持元字段。
为了支持它们,此 API 已更新,允许从任何端点链式使用 ->metafields

这不一定总是有效,因为并非每个端点都支持元字段,不支持元字段的端点将导致 404

以下是支持元字段的全部端点示例。

// 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);

与 Laravel 一起使用

单一商店应用程序

在您的 config/app.php

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

对于单店使用 OAuth 的私用应用程序(环境令牌)

Dan\Shopify\Integrations\Laravel\ShopifyServiceProvider::class,

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

如果您的应用程序仅与单个商店交互,有一个 Facade 可能很有用。

'Shopify' => Dan\Shopify\Integrations\Laravel\ShopifyFacade::class,

对于 Facade 的使用,将以下变量替换为您的 .env

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

可选地替换以下变量在您的 .env

为空或 admin 默认为最旧的受支持的 API,了解更多

SHOPIFY_API_BASE="admin/api/2022-07"

使用 Facade 给您 Dan\Shopify\Shopify

它将使用您在 config/shopify.php 中设置的商店和令牌进行实例化

查看上面的 基本用法,使用 Facade 的方式几乎相同,只是您只与配置中的一个商店交互。

// 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 应用程序

创建公共应用程序使用 OAuth,遵循 Shopify 文档创建您的认证 URL,并使用以下辅助程序使用回调中的代码获取访问令牌。

获取用于重定向响应的令牌。

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

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

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

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

验证应用程序 Webhook Hmac

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

贡献者

待办事项

  • Artisan 命令以创建令牌

许可证

MIT。