【发布时间】:2015-10-06 00:12:24
【问题描述】:
我正在尝试使用 Guzzle 5 连接到 WooCommerce API(Guzzle 6 似乎没有 oAuth 选项 o.O)。 Woocommerce requires the oAuth authentication method 工作。
这是我正在使用的代码:
<?php
/**
* Example of usage of Guzzle 5 to get information
* from a WooCommerce Store.
*/
require('../vendor/autoload.php');
use GuzzleHttp\Client;
use GuzzleHttp\Subscriber\Oauth\Oauth1;
use GuzzleHttp\Exception\RequestException;
$consumer_key = 'my_consumer_key'; // Add your own Consumer Key here
$consumer_secret = 'my_consumer_secret'; // Add your own Consumer Secret here
$store_url = 'http://example.com'; // Add the home URL to the store you want to connect to here
$api_path = '/wc-api/v2/';
$api_end_point = [
'root' => '',
'orders' => 'orders'
];
$base_uri = $store_url . $api_path;
$client = new Client([
'base_url' => $base_uri,
'defaults' => ['auth' => 'oauth']
]);
$oauth = new Oauth1([
'consumer_key' => $consumer_key,
'consumer_secret' => $consumer_secret,
'request_method' => 'query'
]);
$client->getEmitter()->attach($oauth);
try
{
$res = $client->get($api_end_point['orders']);
}
catch (RequestException $e)
{
$res = $e;
if ($e->hasResponse())
{
$res = $e->getResponse();
}
}
print_r($res);
echo $res->getStatusCode();
// "200"
echo $res->getHeader('content-type');
// 'application/json; charset=utf8'
echo $res->getBody();
// {"type":"User"...'
此代码返回一个
woocommerce_api_authentication_error:无效签名 - 提供 签名不匹配
使用纯 curl 函数(使用 this package,我在其中添加了一些我发现 here 的函数),它可以工作,我得到所有订单和我想要的其他数据。
其他一些细节
要使用 Guzzle 5 和 oAuth,我使用了这些作曲家包:
"require": {
"guzzlehttp/guzzle": "~5.0"
},
"require-dev": {
"guzzlehttp/oauth-subscriber": "~0.2",
},
似乎在创建签名时有些不同之处:the library I've used until now 创建的签名有效,但 Guzzle 的 oAuth 插件 (using the method getSignature()) 创建的签名无效,我不是这样曾经使用 oAuth 来查找错误。有人可以帮我找出问题吗?
【问题讨论】: