offshoot/vend-php

此包已被弃用且不再维护。未建议替换包。

PHP 的简单 Vend API 客户端

1.0.0 2015-02-06 17:00 UTC

This package is not auto-updated.

Last update: 2023-01-21 07:38:32 UTC


README

PHP 中简单的 Vend API 客户端。

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

要求

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

开发要求

  • phpunit/phpunit 3.7

入门

使用 Composer 安装 vend-php

如果您的项目根目录中尚未存在,请创建一个 composer.json 文件,并需要 vend-php

{
  "require": {
    "offshoot/vend-php": "~1.0"
  }
}

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

使用 cURL

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

您可以将此作为依赖项添加到 composer 文件中。您的 composer.json 可能如下所示

{
  "require": {
    "offshoot/vend-php": "~1.0",
    "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 文件

用法

身份验证

获取访问令牌

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

$pathToCertificateFile = 'vendor/haxx-se/curl/cacert.pem';
$httpClient = new \Offshoot\HttpClient\CurlHttpClient($pathToCertificateFile);
$redirector = new \Offshoot\Redirector\HeaderRedirector();

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

$authenticate->forStoreName('mycoolstore')
    ->usingClientId('XXX1234567890') // get this from your Vend Account
    ->andReturningTo('http://wherever.you/like')
    ->initiateLogin();

这会将您的用户重定向到 Vend 登录屏幕,他们需要使用 Vend 凭据进行身份验证。完成此操作后,Vend 将向您的重定向 URI 发送 GET 请求,该 URI 将类似于

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

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

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

    // exchange the token
    $accessToken = $authenticate->forStoreName('mycoolstore')
        ->usingClientId('XXX1234567890') // get this from your Vend Account
        ->usingClientSecret('ABC123XYZ') // get this from your Vend Account
        ->andReturningTo('http://wherever.you/like')
        ->toExchange($_GET['code']);

}

刷新访问令牌

待定

与 Vend API 交互

设置 API 客户端

$vend = new \Vend\Api\Client($httpClient);
$vend->setStoreName('mycoolstore');
$vend->setAccessToken($accessToken);

贡献

欢迎贡献。只需将仓库分叉并发送拉取请求。请确保在拉取请求中包含测试覆盖率。您可以在 此处 了解更多关于拉取请求的信息

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

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

许可证

此库是根据GPL 3.0许可证发布的

致谢

感谢Bruce Aldridge为其初始代码库的开发。