yorki/php-shopify

Shopify API 的 PHP SDK

v1.1.19 2021-06-15 17:13 UTC

README

Build Status Monthly Downloads Total Downloads Latest Stable Version Latest Unstable Version License Hire

PHPShopify 是 Shopify API 的简单 SDK 实现。它帮助以面向对象的方式访问 API。

安装

使用 Composer 安装

composer require phpclassic/php-shopify

要求

PHPShopify 使用 curl 扩展来处理 HTTP 调用。因此,您需要安装并启用 PHP 中的 curl 扩展。

但是,如果您更喜欢使用任何其他可用的包库来处理 HTTP 调用,您可以通过修改 PHPShopify\HttpRequestJson 类中的每个 get()post()put()delete() 方法中的一行轻松实现。

您可以将额外的 curl 配置传递给 ShopifySDK

$config = array(
    'ShopUrl' => 'yourshop.myshopify.com',
    'ApiKey' => '***YOUR-PRIVATE-API-KEY***',
    'Password' => '***YOUR-PRIVATE-API-PASSWORD***',   
    'Curl' => array(
        CURLOPT_TIMEOUT => 10,
        CURLOPT_FOLLOWLOCATION => true
    )
);

PHPShopify\ShopifySDK::config($config);

用法

您可以使用 PHPShopify 以非常简单的面向对象方式使用。

配置 ShopifySDK

如果您正在使用自己的私有 API(除 GraphQL 外),请提供 ApiKey 和密码。

$config = array(
    'ShopUrl' => 'yourshop.myshopify.com',
    'ApiKey' => '***YOUR-PRIVATE-API-KEY***',
    'Password' => '***YOUR-PRIVATE-API-PASSWORD***',
);

PHPShopify\ShopifySDK::config($config);

对于第三方应用程序,请使用永久访问令牌。

对于 GraphQL,需要访问令牌。如果您正在使用 GraphQL 的私有 API,请在此处使用您的密码作为访问令牌。

$config = array(
    'ShopUrl' => 'yourshop.myshopify.com',
    'AccessToken' => '***ACCESS-TOKEN-FOR-THIRD-PARTY-APP***',
);

PHPShopify\ShopifySDK::config($config);
如何获取商店的永久访问令牌?

有一个 AuthHelper 类可以帮助您使用 oAuth 从商店获取永久访问令牌。

  1. 首先,您需要使用额外的参数 SharedSecret 配置 SDK。
$config = array(
    'ShopUrl' => 'yourshop.myshopify.com',
    'ApiKey' => '***YOUR-PRIVATE-API-KEY***',
    'SharedSecret' => '***YOUR-SHARED-SECRET***',
);

PHPShopify\ShopifySDK::config($config);
  1. 创建身份验证请求

重定向 URL 必须由您的应用管理员白名单列出,作为 应用程序重定向 URL 之一。

//your_authorize_url.php
$scopes = 'read_products,write_products,read_script_tags,write_script_tags';
//This is also valid
//$scopes = array('read_products','write_products','read_script_tags', 'write_script_tags'); 
$redirectUrl = 'https://yourappurl.com/your_redirect_url.php';

\PHPShopify\AuthHelper::createAuthRequest($scopes, $redirectUrl);

如果您想使函数返回身份验证 URL 而不是自动重定向,可以将参数 $return(第 5 个参数)设置为 true

\PHPShopify\AuthHelper::createAuthRequest($scopes, $redirectUrl, null, null, true);
  1. 在应用授权后重定向到 $redirectUrl 时获取访问令牌。
//your_redirect_url.php
PHPShopify\ShopifySDK::config($config);
$accessToken = \PHPShopify\AuthHelper::getAccessToken();
//Now store it in database or somewhere else

您可以使用同一页面创建请求和获取访问令牌(重定向 URL)。在这种情况下,只需在调用 createAuthRequest() 方法时跳过第二个参数 $redirectUrl。AuthHelper 类将为您完成剩余的操作。

//your_authorize_and_redirect_url.php
PHPShopify\ShopifySDK::config($config);
$accessToken = \PHPShopify\AuthHelper::createAuthRequest($scopes);
//Now store it in database or somewhere else

获取 ShopifySDK 对象

$shopify = new PHPShopify\ShopifySDK;

您可以在实例化对象时提供配置作为参数(如果您还没有通过调用 config() 方法进行配置)

$shopify = new PHPShopify\ShopifySDK($config);
现在,您可以通过对象化方式调用对象中的资源来执行 get()post()put()delete()。所有资源都按照 Shopify API 参考中使用的名称命名。(请参阅下面的资源映射。)

如果请求成功,所有请求都返回一个数组(可以是一个单独的资源数组或多个资源的数组)。当不期望有结果时(例如 DELETE 请求),将返回一个空数组。

  • 获取所有产品列表(GET 请求)
$products = $shopify->Product->get();
  • 通过 ID 获取任何特定产品(GET 请求)
$productID = 23564666666;
$product = $shopify->Product($productID)->get();

您还可以通过使用 URL 参数(如 Shopify API 参考中为每个特定资源指定的)来过滤结果。

  • 例如,获取指定日期和时间之后的已取消订单列表(fields 指定了每行要渲染的数据列)
$params = array(
    'status' => 'cancelled',
    'created_at_min' => '2016-06-25T16:15:47-04:00',
    'fields' => 'id,line_items,name,total_price'
);

$orders = $shopify->Order->get($params);
  • 创建新订单(POST 请求)
$order = array (
    "email" => "foo@example.com",
    "fulfillment_status" => "unfulfilled",
    "line_items" => [
      [
          "variant_id" => 27535413959,
          "quantity" => 5
      ]
    ]
);

$shopify->Order->post($order);

请注意,您不需要将数据数组包装在资源键中(在本例中为 order),这是 Shopify API 预期的语法。此 SDK 会自动处理。

  • 更新订单(PUT 请求)
$updateInfo = array (
    "fulfillment_status" => "fulfilled",
);

$shopify->Order($orderID)->put($updateInfo);
  • 删除 Webhook(DELETE 请求)
$webHookID = 453487303;

$shopify->Webhook($webHookID)->delete();

可以使用嵌套方式使用子资源。

在尝试获取任何子资源时,您必须提供父资源的 ID。

  • 例如,获取产品的图像(GET 请求)
$productID = 23564666666;
$productImages = $shopify->Product($productID)->Image->get();
  • 为客户添加新地址(POST 请求)
$address = array(
    "address1" => "129 Oak St",
    "city" => "Ottawa",
    "province" => "ON",
    "phone" => "555-1212",
    "zip" => "123 ABC",
    "last_name" => "Lastnameson",
    "first_name" => "Mother",
    "country" => "CA",
);

$customerID = 4425749127;

$shopify->Customer($customerID)->Address->post($address);
  • 创建履行事件(POST 请求)
$fulfillmentEvent = array(
    "status" => "in_transit"
);

$shopify->Order($orderID)->Fulfillment($fulfillmentID)->Event->post($fulfillmentEvent);
  • 更新博客文章(PUT 请求)
$blogID = 23564666666;
$articleID = 125336666;
$updateArtilceInfo = array(
    "title" => "My new Title",
    "author" => "Your name",
    "tags" => "Tags, Will Be, Updated",
    "body_html" => "<p>Look, I can even update through a web service.<\/p>",
);
$shopify->Blog($blogID)->Article($articleID)->put($updateArtilceInfo);
  • 从特定博客中删除任何特定文章(DELETE请求)
$blogArticle = $shopify->Blog($blogID)->Article($articleID)->delete();

GraphQL 版本1.1

GraphQL Admin API是一个基于GraphQL的REST Admin API的替代品,它使得Shopify管理功能的可用性通过单个GraphQL端点。支持的所有类型的完整集合可以在GraphQL Admin API参考中找到。您可以直接调用GraphQL资源,并使用GraphQL字符串进行POST请求。

GraphQL Admin API需要访问令牌来发送认证请求。您可以通过创建私有应用并使用该应用的API密码,或者通过遵循OAuth授权流程来获取访问令牌。请参阅GraphQL认证指南

$graphQL = <<<Query
query {
  shop {
    name
    primaryDomain {
      url
      host
    }
  }
}
Query;

$data = $shopify->GraphQL->post($graphQL);
变量

如果您想使用GraphQL变量,您需要将变量放入数组中,并将其作为post()方法的第4个参数提供。第2个和第3个参数在GraphQL中没有用途,但保留以保持与其他请求的相似性,您可以将其保留为null。以下是一个示例:

$graphQL = <<<Query
mutation ($input: CustomerInput!) {
  customerCreate(input: $input)
  {
    customer {
      id
      displayName
    }
    userErrors {
      field
      message
    }
  }
}
Query;

$variables = [
  "input" => [
    "firstName" => "Greg",
    "lastName" => "Variables",
    "email" => "gregvariables@teleworm.us"
  ]
]
$shopify->GraphQL->post($graphQL, null, null, $variables);
GraphQL构建器

此SDK仅接受GraphQL字符串作为输入。您可以从Shopify GraphQL构建器构建您的GraphQL。

资源映射

一些资源可以直接访问,一些资源只能通过父资源访问,而一些资源两种方式都可以访问。建议您查看有关每个资源的Shopify API参考页面中的详细信息。这里每个资源名称都链接到相关的Shopify API参考页面。

请仅通过列出的资源映射使用资源。尝试直接获取只能通过父资源访问的资源可能会导致错误。

自定义操作

有几个操作方法可以在不直接调用 get()post()put()delete() 方法的情况下调用,但最终会调用这些方法之一的自定义调用。

  • 例如,获取总产品数
$productCount = $shopify->Product->count();
  • 将地址设为客户默认地址。
$shopify->Customer($customerID)->Address($addressID)->makeDefault();
  • 搜索居住在美国的国家并使用关键词 "Bob" 的客户。
$shopify->Customer->search("Bob country:United States");

自定义操作列表

自定义方法针对某些资源,这些资源可能对其他资源不可用。建议您查看每个操作的 Shopify API 参考页面中的详细信息。我们在此仅列出可用的操作及其简要信息。每个操作名称都链接到 Shopify API 参考中的示例,其中包含更多详细信息。

Shopify API 功能头

在使用SDK时发送 X-Shopify-Api-Features 头部,您可以使用以下方法

$config['ShopifyApiFeatures'] = ['include-presentment-prices'];
$shopify = new PHPShopify\ShopifySDK($config);

参考

付费支持

您可以雇佣此SDK的作者,以使用PHPShopify SDK设置您的项目。

在Upwork上雇佣

赞助者

通过每月捐款支持我们,帮助我们继续活动。[成为赞助者]

赞助商

成为赞助商,并在我们的GitHub README中放置您的标志,并提供到您网站的链接。[成为赞助商]