mfdmc/shopify

Shopify PHP SDK,用于与 Shopify API 交互(从 donutdan4114/shopify 克隆而来)。

dev-master 2018-11-20 20:05 UTC

This package is auto-updated.

Last update: 2024-09-21 21:16:54 UTC


README

这是一个简单的 Shopify PHP SDK,适用于私有应用程序,以便轻松与 Shopify API 交互。
Travis Build Status

Shopify API 文档 | Packagist | 构建状态

特性包括

  • 轻松地获取、PUT、POST 和 DELETE 资源
  • 处理和验证传入的 webhooks
  • 自动速率限制以避免 API 调用出错

设置/安装

使用 guzzlehttp/guzzle。您可以通过运行以下命令来包含此库
composer require donutdan4114/shopify@dev

私有和公共应用程序

您可以使用此库创建私有或公共应用程序。使用私有应用程序更容易,因为它们不需要 access_token。但是,如果您想创建一个公开可访问的应用程序,则必须使用公共应用程序系统。

私有应用程序

只需使用 shop_domainapi_keypasswordshared_secret 实例化私有应用程序。

$client = new Shopify\PrivateApp($SHOPIFY_SHOP_DOMAIN, $SHOPIFY_API_KEY, $SHOPIFY_PASSWORD, $SHOPIFY_SHARED_SECRET);
$result = $client->get('shop');

公共应用程序

您必须首先设置公共应用程序。请查看文档。您需要一个授权 URL。

session_start();
$client = new Shopify\PublicApp($_GET['shop'], $APP_API_KEY, $APP_SECRET);

// You set a random state that you will confirm later.
$random_state = 'client-id:' . $_SESSION['client_id'];

$client->authorizeUser('[MY_DOMAIN]/redirect.php', [
  'read_products',
  'write_products',
], $random_state);

此时,用户将被带到他们的商店以授权应用程序使用他们的信息。
如果用户接受,他们将被带到重定向 URL。

session_start();
$client = new Shopify\PublicApp($_GET['shop'], $APP_API_KEY, $APP_SECRET);

// Used to check request data is valid.
$client->setState('client-id:' . $_SESSION['client_id']);

if ($token = $client->getAccessToken()) {
  $_SESSION['shopify_access_token'] = $token;
  $_SESSION['shopify_shop_domain'] = $_GET['shop'];
  header("Location: dashboard.php");
}
else {
  die('invalid token');
}

此时,在 dashboard.php 中,您可以通过设置 access_token 开始进行 API 请求。

session_start();
$client = new Shopify\PublicApp($_SESSION['shopify_shop_domain'], $APP_API_KEY, $APP_SECRET);
$client->setAccessToken($_SESSION['shopify_access_token']);
$products = $client->getProducts();

方法

GET

从 API 获取资源信息。

$client = new Shopify\PrivateApp($SHOPIFY_SHOP_DOMAIN, $SHOPIFY_API_KEY, $SHOPIFY_PASSWORD, $SHOPIFY_SHARED_SECRET);
$result = $client->get('shop');

$result 是一个 JSON 解码的 stdClass

object(stdClass)#33 (1) {
  ["shop"]=>
  object(stdClass)#31 (44) {
    ["id"]=>
    int([YOUR_SHOP_ID])
    ["name"]=>
    string(15) "[YOUR_SHOP_NAME]"
    ["email"]=>
    string(22) "[YOUR_SHOP_EMAIL]"
    ["domain"]=>
    string(29) "[YOUR_SHOP_DOMAIN]"
    ...
  }
}

通过传递查询参数获取产品 ID

$result = $client->get('products', ['query' => ['fields' => 'id']]);
foreach($result->products as $product) {
  print $product->id;
}

POST

使用 POST 请求创建新内容。

$data = ['product' => ['title' => 'my new product']];
$result = $client->post('products', $data);

PUT

使用给定的 ID 更新现有内容。

$data = ['product' => ['title' => 'updated product name']];
$result = $client->put('products/' . $product_id, $data);

DELETE

轻松地使用给定的 ID 删除资源。

$client->delete('products/' . $product_id);

简单包装器

为了更容易地处理常见的 API 资源,有几个简写函数。

// Get shop info.
$shop_info = $client->getShopInfo();

// Get a specific product.
$product = $client->getProduct($product_id);

// Delete a specific product.
$client->deleteProduct($product_id);

// Create a product.
$product = $client->createProduct(['title' => 'my new product']);

// Count products easily.
$count = $client->getProductsCount(['updated_at_min' => time() - 3600]);

// Easily get all products without having to worry about page limits.
$products = $client->getProducts();
// This will fetch all products and will make multiple requests if necessary.
// You can easily supply filter arguments.
$products = $client->getProducts(['query' => ['vendor' => 'MY_VENDOR']]);

// For ease-of-use, you should use the getResources() method to automatically handle Shopify's pagination.
$orders = $client->getResources('orders', ['query' => ['fields' => 'id,billing_address,customer']]);
// This will ensure that if there are over 250 orders, you get them all returned to you.

// If efficiency and memory limits are a concern,  you can loop over results manually.
foreach ($this->client->getResourcePager('products', 25) as $product) {
  // Fetches 25 products at a time.
  // If you have 500 products, this will create 20 separate requests for you.
  // PHP memory will only be storing 25 products at a time, which keeps thing memory-efficient.
}

解析传入的 webhooks

如果您已在网站上设置了一个路由来接受 Shopify webhooks,则可以轻松解析数据并验证内容。有两种验证 webhooks 的方法:手动或使用客户端。

// Process webhook manually.
$webhook = new Shopify\IncomingWebhook($SHOPIFY_SHARED_SECRET);
try {
  $webhook->validate();
  $data = $webhook->getData();
} catch (Shopify\WebhookException $e) {
  // Errors means you should not process the webhook data.
  error_log($e->getMessage());
}

// Process webhook using the $client.
try {
  $data = $client->getIncomingWebhook($validate = TRUE);
} catch (Shopify\ClientException $e) {
  error_log($e->getMessage());
}
if (!empty($data)) {
  // Do something with the webhook data.
}

错误处理

任何 API 错误都会抛出 Shopify\ClientException 实例。

try {
  $response = $client->put('products/BAD_ID');
} catch (Shopify\ClientException $e) {
  // Get request errors.
  error_log($e->getErrors());
  // Get last response object.
  $last_response = $e->getLastResponse();
  $code = $e->getCode();
  $code = $last_response->getStatusCode();
}

API 限制处理

此类可以根据 Shopify 的“漏桶”算法为您处理 API 速率限制。
它将自动减慢请求速度,以避免触发速率限制器。
您可以通过以下方式禁用它

$client->rate_limit = FALSE;

您可以使用 $client->getCallLimit()$client->callLimitReached() 方法使用自己的速率限制逻辑。

测试

可以使用 phpunit 运行测试。
由于测试实际上会修改连接的商店,因此您必须通过设置 SHOPIFY_ALLOW_TESTS 环境变量为 TRUE 来显式允许运行测试。
如果没有这样做,您将收到如下消息

Shopify tests cannot be run.
Running Shopify tests will delete all connected store info.
Set environment variable SHOPIFY_ALLOW_TESTS=TRUE to allow tests to be run.