nicoren/php-shopify

Shopify API 的 PHP SDK

2.0.4 2021-02-10 10:28 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() 方法中的 1 行来实现。

用法

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

配置 ShopifySDK

如果您使用自己的私有 API,请提供 ApiKey 和密码。

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

PHPShopify\ShopifySDK::config($config);

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

$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() 方法时跳过第 2 个参数 $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 v1.1

GraphQL管理API是一个基于GraphQL的REST管理API的替代方案,使得Shopify管理功能可通过单个GraphQL端点访问。支持的完整类型集可在GraphQL管理API参考中找到。您可以简单地调用GraphQL资源,并使用GraphQL字符串进行POST请求。

GraphQL管理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参考中的一个示例,其中包含更多详细信息。

参考

付费支持

您可以使用PHPShopify SDK的作者来设置您的项目。 [雇佣作者]

在Upwork上雇佣

支持者

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

赞助商

成为赞助商,让您的标志出现在我们的GitHub README上,并提供到您网站的链接。[成为赞助商]