offshoot/shopify-php

此包已被废弃,不再维护。未建议替代包。

PHP中的简单Shopify API客户端

0.10.0 2015-06-01 06:51 UTC

This package is not auto-updated.

Last update: 2021-04-26 10:10:54 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

这是一个简单的Shopify API客户端。

此开发流的规范仓库是 https://github.com/TeamOffshoot/shopify-php

此API客户端仍处于预1.0状态,因此您可能

  • 会遇到一些错误(请自由提交带有错误修复和测试覆盖的拉取请求)
  • 在v0.10和v1.0之间可能有一些破坏性的API更改

需求

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

开发需求

  • phpunit/phpunit 3.7

入门

通过 Composer 安装 shopify-php

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

{
  "require": {
    "offshoot/shopify-php": "0.10.x"
  }
}

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

使用cURL

如果您使用的是基于cURL的HttpClient,如 CurlHttpClient,则需要包含位于 http://curl.haxx.se/docs/caextract.html 的 cacert.pem 文件

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

{
  "require": {
    "offshoot/shopify-php": "0.10.x",
    "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",
    )
));

待办事项:实现PUT和DELETE功能

贡献

欢迎贡献。只需fork仓库并发送pull request。请确保在你的pull request中包含测试覆盖率。你可以在这里了解更多关于Pull Request的信息 这里

为了运行测试套件,请确保已经通过composer安装了开发依赖项。然后在你的命令行中,简单运行

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

许可证

此库在MIT许可证下发布

致谢

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