cadicvnn/shopify-php

PHP 中的简单 Shopify API 客户端

0.9.2 2018-09-21 07:06 UTC

This package is auto-updated.

Last update: 2024-09-15 16:13:11 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

PHP 中的简单 Shopify API 客户端。

此开发流的官方仓库为 https://github.com/TeamOffshoot/shopify-php

此 API 客户端目前处于预 1.0 状态,因此您可以期待

  • 一些错误(请随意提交带有错误修复和测试覆盖的 pull request)
  • 在 v0.10 和 v1.0 之间可能有一些破坏性 API 变更

要求

  • PHP 5.3(或更高版本)
  • ext-curl, ext-json

开发要求

  • phpunit/phpunit 3.7

入门

通过 Composer 安装 shopify-php

如果您在项目根目录中没有 composer.json 文件,请创建一个并要求 shopify-php

{
  "require": {
    "cadicvnn/shopify-php": "dev-master"
  }
}

要了解更多关于 Composer 的信息,包括完整的安装过程,请访问 https://getcomposer.org.cn/

使用 cURL

如果您正在使用基于 cURL 的 HttpClient,如 CurlHttpClient,则希望包含可从 http://curl.haxx.se/docs/caextract.html 找到的 cacert.pem 文件

您可以在 composer 文件中添加此依赖项。您的 composer.json 可能看起来像这样

{
  "require": {
    "cadicvnn/shopify-php": "dev-master",
    "haxx-se/curl": "1.0.0"
  },
  "repositories": [
    {
      "type": "package",
      "package": {
        "name": "haxx-se/curl",
        "version": "1.0.0",
        "dist": {
          "url": "http://curl.haxx.se/ca/cacert.pem",
          "type": "file"
        }
      }
    }
  ]
}

您将能够在 vendor/haxx-se/curl/cacert.pem 中找到 cacert.pem 文件

用法

身份验证

如果您还没有 Shopify API 永久访问令牌,您需要首先通过 Shopify API 进行身份验证

$pathToCertificateFile = "vendor/haxx-se/curl/cacert.pem";
$httpClient = new \Shopify\HttpClient\CurlHttpClient($pathToCertificateFile);

$redirector = new \Shopify\Redirector\HeaderRedirector();

$authenticate = new \Shopify\Api\AuthenticationGateway(
    $httpClient, $redirector
);

$authenticate->forShopName('mycoolshop')
    ->usingClientId('XXX1234567890') // get this from your Shopify Account
    ->withScope(array('write_products', 'read_orders'))
    ->andReturningTo("http://wherever.you/like")
    ->initiateLogin();

这会将您的用户重定向到 Shopify 登录屏幕,他们需要使用 Shopify 凭据进行身份验证。完成之后,Shopify 将对您的重定向 URI 执行 GET 请求,该 URI 将如下所示

GET http://wherever.you/like?code=TEMP_TOKEN

您的应用程序需要捕获请求中的 code 查询参数,并使用该参数从 Shopify 获取永久访问令牌

$client = new Shopify\Api\Client($httpClient);
$client->setClientSecret('ABC123XYZ');

// validate the Shopify Request
if ($client->isValidRequest($_GET)) {

    // exchange the token
    $permanentAccessToken = $authenticate->forShopName('mycoolshop')
        ->usingClientId('XXX1234567890')
        ->usingClientSecret('ABC123XYZ')
        ->toExchange($_GET['code']);

}

TODO:在交换过程中构建请求验证

TODO:让 AuthenticationGateway 扩展 Api\Client

与 Shopify API 交互

一旦您有了有效的 Shopify 永久访问令牌,您就可以开始调用 Shopify API 了

首先设置 Shopify API 客户端的实例。

$client = new \Shopify\Api\Client($httpClient);
$client->setAccessToken($permanentAccessToken);
$client->setClientSecret('ABC123XYZ');
$client->setShopName('mycoolshop');

然后您就可以开始与 Shopify API 交互了。也许您想获取您商店中的所有产品

$products = $client->get('/admin/products.json', array(
    'collection_id' => '987654321'
));

也许您想获取特定订单的详细信息

$order = $client->get('/admin/orders/123456789.json');

或者也许您想创建一个新订单

$order = $client->post('/admin/orders.json', array(
    'order' => array(
        'line_items' => array(
            0 => array(
                'grams' => 1300,
                'price' => 74.99,
                'quantity' => 3,
                'title' => "Big Brown Bear Boots",
            ),
        ),
        'tax_lines' => array(
            0 => array(
                'price' => 29.25,
                'rate' => 0.13,
                'title' => "HST",
            ),
        ),
        'transactions' => array(
            0 => array(
                'amount' => 254.22,
                'kind' => "sale",
                'status' => "success",
            )
        ),
        'total_tax' => 29.25,
        'currency' => "CAD",
    )
));

TODO:实现 PUT 和 DELETE 功能

贡献

欢迎贡献。只需分叉存储库并发送 pull request。请确保在 pull request 中包含测试覆盖。您可以在此处了解有关 Pull Requests 的更多信息 here

为了运行测试套件,请确保已通过 composer 安装了开发依赖项。然后从您的命令行运行

vendor/bin/phpunit --bootstrap tests/bootstrap.php tests/

许可

此库在 MIT 许可证 下发布

致谢

感谢 Sandeep Shetty 对初始代码库的开发。