Aer*_*dir 4 php curl oauth guzzle
我正在尝试使用 Guzzle 5 连接到 WooCommerce API(Guzzle 6 似乎没有 oAuth 选项 oO)。Woocommerce需要 oAuth 身份验证方法才能工作。
这是我正在使用的代码:
<?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"...'
Run Code Online (Sandbox Code Playgroud)
这段代码返回一个
woocommerce_api_authentication_error:签名无效 - 提供的签名不匹配
相反,使用纯curl函数(使用这个包,我在其中放置了我在这里找到的一些函数),它可以工作,并且我可以获得我想要的所有订单和其他数据。
其他一些细节
要使用 Guzzle 5 和 oAuth,我使用这些 Composer 包:
"require": {
"guzzlehttp/guzzle": "~5.0"
},
"require-dev": {
"guzzlehttp/oauth-subscriber": "~0.2",
},
Run Code Online (Sandbox Code Playgroud)
似乎在创建签名时有些不同:由我到目前为止使用的库创建的签名可以工作,但是由 Guzzle 的 oAuth 插件(使用方法getSignature())创建的签名则不行,我不太习惯使用oAuth来查找错误。有人可以帮我找出问题所在吗?
更新@Aerendir 答案
@Aerendir 在他的拉取请求中写道:
就我而言,我在尝试连接到 WooCommerce API 版本 2 时进行了编辑,但该版本的 API 没有正确实现 OAuth Core 1.0a 规范。事实上,他们在 API 的第 3 版中修复了这个问题。请参阅 V3 和旧版本之间的差异。
来源:https ://github.com/guzzle/oauth-subscriber/pull/42#issuecomment-185631670
因此,为了使他的答案正常工作,我们需要使用wc-api/ v3 /而不是wc-api/v2/。
以下代码适用于 Guzzle 6、oauth 和 WooCommerce api v3:
use GuzzleHttp\Client,
GuzzleHttp\HandlerStack,
GuzzleHttp\Handler\CurlHandler,
GuzzleHttp\Subscriber\Oauth\Oauth1;
$url = 'http://localhost/WooCommerce/';
$api = 'wc-api/v3/';
$endpoint = 'orders';
$consumer_key = 'ck_999ffa6b1be3f38058ed83e5786ac133e8c0bc60';
$consumer_secret = 'cs_8f6c96c56a7281203c2ff35d71e5c4f9b70e9704';
$handler = new CurlHandler();
$stack = HandlerStack::create($handler);
$middleware = new Oauth1([
'consumer_key' => $consumer_key,
'consumer_secret' => $consumer_secret,
'token_secret' => '',
'token' => '',
'request_method' => Oauth1::REQUEST_METHOD_QUERY,
'signature_method' => Oauth1::SIGNATURE_METHOD_HMAC
]);
$stack->push($middleware);
$client = new Client([
'base_uri' => $url . $api,
'handler' => $stack
]);
$response = $client->get( $endpoint, [ 'auth' => 'oauth' ] );
echo $response->getStatusCode() . '<br>';
echo $response->getHeaderLine('content-type') . '<br>';
echo $response->getBody();
Run Code Online (Sandbox Code Playgroud)