phpclassic/php-shopify

Shopify API 的 PHP SDK

v1.2.10 2024-07-02 14:27 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 行来轻松地这样做。

您可以将额外的 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,需要 AccessToken。如果您正在使用 GraphQL 的私有 API,请在此处使用您的密码作为 AccessToken。

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

PHPShopify\ShopifySDK::config($config);

您可以通过添加到配置数组中,使用特定的 Shopify API 版本

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

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(第五个参数)设置为 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参考中的一个示例,其中包含更多详细信息。

履行请求资源 - 包括操作

  • 映射的履行订单 -> 履行请求
  • 映射订单(id) -> 履行订单
// Requesting the FulfilmentOrder for a given order
$fo = $client->Order("1234567890")->FulfillmentOrder()->get();

// Requesting assigned fulfillment orders (with status fulfillment_requested)
$shopify->AssignedFulfillmentOrder()->get(["assignment_status" => "fulfillment_requested"]);

// Creating a FulfilmentRequest
// Follow instructions to get partial fulfilments
$fr = $client->FulfillmentOrder('0987654321')->FulfillmentRequest->post([]);

// Accepting \ Rejecting a FulfilmentRequest
$fr = $client->FulfillmentOrder('0987654321')->FulfillmentRequest->accept();
$fr = $client->FulfillmentOrder('0987654321')->FulfillmentRequest->reject();

// Communicating fulfillment
$client->Fulfillment->post($body)

Shopify API 功能头

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

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

参考

付费支持

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

在Upwork上雇佣

支持者

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

赞助商

成为赞助商,并在Github上的README中放置您的标志,附带指向您网站的链接。[成为赞助商]