vladimircatrici/shopify-api

通过 REST API 操作 Shopify 数据的库

v0.2.8 2024-01-19 02:14 UTC

README

这是一个简单的 PHP 库,它提供了一种快速便捷的方式与 Shopify REST API 进行交互。它使用 Guzzle 作为 HTTP 客户端。

安装

composer require vladimircatrici/shopify

使用

初始化

require_once 'vendor/autoload.php';
use VladimirCatrici\Shopify;
Shopify\ClientManager::setConfig('default', [
    'domain' => 'your-shop-handle',
    'access_token' => 'your-access-token',
]);
$api = Shopify\ClientManager::get('default');

配置

您可以向 ClientManager 传递一些额外的选项。

  • api_version (默认: 支持的最老稳定版本) 您想要使用的 Shopify API 版本。
    了解 Shopify 的 API 版本控制 的更多信息。

  • max_attempts_on_server_errors (默认: 1)
    尝试执行请求的次数。这很有用,因为 Shopify 有时可能会响应 500 错误。我建议将其设置为 23。默认值是 1

  • max_attempts_on_rate_limit_errors (默认: 1)
    在收到 429 Too Many Connections 错误时尝试执行请求的次数。如果其他应用程序使用相同的 API 密钥,可能会导致超过速率限制。建议的值应该低于 10。

  • max_limit_rate (默认: 0.5)
    介于 0 和 1 之间的数字,描述客户端在进入睡眠之前应该达到的最大限制速率。请参阅 max_limit_rate_sleep_sec 选项。

  • max_limit_rate_sleep_sec (默认: 1)
    当 API 达到在 max_limit_rate 选项中指定的最大 API 限制速率时,休眠的秒数。

基本用法

客户端实现了 Shopify REST API 支持的所有 4 种 HTTP 方法。方法名称为

  • get(string $endpoint, array $options)
  • post(string $endpoint, array $data)
  • put(string $endpoint, array $data)
  • delete(string $endpoint)

$endpoint 参数必须始终是一个字符串,表示 Shopify API 端点。它不应包含开始处的 /admin/api/#{api_version}/ 部分。路径末尾也不需要 .json。例如,如果 Shopify 文档显示端点路径为 GET /admin/api/#{api_version}/orders.json,则可以使用

$api->get('orders');

下面有更多示例

获取项目

$numProducts = $api->get('products/count'); // int
$products = $api->get('products'); // array
foreach ($products as $product) {
  echo sprintf('#%d. %s<br>', 
    $product['id'], $product['title']);
}

获取第 2 页的 250 项的 idtitle 字段

$products = $api->get('products', [
  'fields' => 'id,title',
  'limit' => 250,
  'page' => 2
]);

获取单个项目

$product = $api->get('products/123456789');
echo sprintf('#%d. %s', $product['id'], $product['title']);

更新项目

$product = $api->put('products/123456789', [
    'title' => 'New title'
]);
echo sprintf('#%d. %s', $product['id'], $product['title']); // #1. New title

创建项目

$product = $api->post('products', [
    'title' => 'New product' 
]);

删除项目

$api->delete('products/123456789');
if ($api->respCode == 200) {
    // Item has been successfully removed
}

集合

您可以使用集合对象从特定端点获取所有项目。这个库可以很好地与基于页面和基于游标的分页一起工作,并根据 API 版本在它们之间切换。

use VladimirCatrici\Shopify\ClientManager;
$api = ClientManager::get('default');
$products = new Collection($api, 'products');
foreach ($products as $product) {
    printf('#%d. %s [$%f], 
        $product['id'], $product['title'], $product['price']
    );
}

Webhooks

您可以使用这个库来监听商店的 webhooks。

use VladimirCatrici\Shopify;
if (Shopify\Webhook::validate('your-webhook-token')) {
    printf('`%s` webhook triggered on your store (%s). Data received: %s', 
        Shopify\Webhook::getTopic(), 
        Shopify\Webhook::getShopDomain(),
        Shopify\Webhook::getData()
    );
} else {
    // Invalid request | Unauthorized webhook | Data corrupted
}

// You can also get webhook data as array right away
$data = Shopify\Webhook::getDataAsArray();
printf('Product ID#%d, product title: %s', 
    $data['id'], $data['title']
);

故障排除

use VladimirCatrici\Shopify\Exception\RequestException;
try {
    $products = $api->get('products');
} catch (RequestException $e) {
    $request = $e->getRequest(); // PSR-7/Request object

    $response = $e->getResponse(); // PSR-7/Response object
    $code = $response->getStatusCode(); // int
    $headers = $response->getHeaders(); // array
    $bodyStream = $response->getBody(); // Stream (PSR-7/StreamInterface)
    $bodyContent = (string) $response->getBody(); // string (or $body->__toString())
    
    // Details of the errors including exception message, request and response details
    echo $e->getDetailsJson();
}