automattic / woocommerce

WooCommerce REST API的PHP包装器

3.1.0 2022-03-18 21:46 UTC

This package is auto-updated.

Last update: 2024-09-13 20:42:04 UTC


README

WooCommerce REST API的PHP包装器。使用此库可以安全地与WooCommerce REST API交互。如果使用HTTPS连接,则此库使用BasicAuth,否则使用Oauth提供安全的WooCommerce连接。

CI status Scrutinizer Code Quality PHP version

安装

composer require automattic/woocommerce

入门指南

按照以下说明生成API凭证(消费者密钥和消费者密钥):http://docs.woocommerce.com/document/woocommerce-rest-api/

查看WooCommerce API端点和可操作的数据:https://woocommerce.github.io/woocommerce-rest-api-docs/

设置

为新的WP REST API集成设置(WooCommerce 2.6或更高版本)

require __DIR__ . '/vendor/autoload.php';

use Automattic\WooCommerce\Client;

$woocommerce = new Client(
  'http://example.com',
  'ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
  'cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
  [
    'version' => 'wc/v3',
  ]
);

客户端类

$woocommerce = new Client($url, $consumer_key, $consumer_secret, $options);

选项

客户端选项

客户端方法

GET

$woocommerce->get($endpoint, $parameters = []);

POST

$woocommerce->post($endpoint, $data);

PUT

$woocommerce->put($endpoint, $data);

DELETE

$woocommerce->delete($endpoint, $parameters = []);

OPTIONS

$woocommerce->options($endpoint);

参数

响应

所有方法在成功时将返回数组,或在失败时抛出HttpClientException错误。

use Automattic\WooCommerce\HttpClient\HttpClientException;

try {
  // Array of response results.
  $results = $woocommerce->get('customers');
  // Example: ['customers' => [[ 'id' => 8, 'created_at' => '2015-05-06T17:43:51Z', 'email' => ...
  echo '<pre><code>' . print_r($results, true) . '</code><pre>'; // JSON output.

  // Last request data.
  $lastRequest = $woocommerce->http->getRequest();
  echo '<pre><code>' . print_r($lastRequest->getUrl(), true) . '</code><pre>'; // Requested URL (string).
  echo '<pre><code>' .
    print_r($lastRequest->getMethod(), true) .
    '</code><pre>'; // Request method (string).
  echo '<pre><code>' .
    print_r($lastRequest->getParameters(), true) .
    '</code><pre>'; // Request parameters (array).
  echo '<pre><code>' .
    print_r($lastRequest->getHeaders(), true) .
    '</code><pre>'; // Request headers (array).
  echo '<pre><code>' . print_r($lastRequest->getBody(), true) . '</code><pre>'; // Request body (JSON).

  // Last response data.
  $lastResponse = $woocommerce->http->getResponse();
  echo '<pre><code>' . print_r($lastResponse->getCode(), true) . '</code><pre>'; // Response code (int).
  echo '<pre><code>' .
    print_r($lastResponse->getHeaders(), true) .
    '</code><pre>'; // Response headers (array).
  echo '<pre><code>' . print_r($lastResponse->getBody(), true) . '</code><pre>'; // Response body (JSON).
} catch (HttpClientException $e) {
  echo '<pre><code>' . print_r($e->getMessage(), true) . '</code><pre>'; // Error message.
  echo '<pre><code>' . print_r($e->getRequest(), true) . '</code><pre>'; // Last request data.
  echo '<pre><code>' . print_r($e->getResponse(), true) . '</code><pre>'; // Last response data.
}

发布历史

  • 2022-03-18 - 3.1.0 - 添加了新的选项以支持来自WP的_methodX-HTTP-Method-Override,支持7+,放弃了对PHP 5的支持。
  • 2019-01-16 - 3.0.0 - 默认关闭了遗留API,并改进了JSON错误处理。
  • 2018-03-29 - 2.0.1 - 修复了在lookForErrors中出现的致命错误。
  • 2018-01-12 - 2.0.0 - 响应从数组更改为stdClass对象。添加了follow_redirects选项。
  • 2017-06-06 - 1.3.0 - 在解码前移除BOM,并为OAuth1.0a添加了对多维数组的支持。
  • 2017-03-15 - 1.2.0 - 添加了user_agent选项。
  • 2016-12-14 - 1.1.4 - 修复了WordPress 4.7兼容性问题。
  • 2016-10-26 - 1.1.3 - 允许设置oauth_timestamp并改进了响应头处理方式。
  • 2016-09-30 - 1.1.2 - 添加了wp_api_prefix选项,允许自定义WP REST API URL前缀。
  • 2016-05-10 - 1.1.1 - 修复了WP REST API的OAuth和错误处理。
  • 2016-05-09 - 1.1.0 - 添加了对WP REST API的支持,添加了方法Automattic\WooCommerce\Client::options并修复了多个头部响应。
  • 2016-01-25 - 1.0.2 - 修复了获取包含非拉丁字符的数据时的错误。
  • 2016-01-21 - 1.0.1 - 在构建请求URL之前对所有OAuth参数进行排序。
  • 2016-01-11 - 1.0.0 - 稳定版发布。