【发布时间】:2021-11-18 19:27:26
【问题描述】:
问题:如何使用 Guzzle 和 guzzle/oauth-subscriber 通过 API 更新 woocommerce 产品的价格
我使用This Question 作为我的参考来让 oauth1 工作以请求数据,效果很好。只是无法锻炼以发送帖子变量。
大多数教程和 guzzle 文档都使用 $client->request('GET', 'http://httpbin.org', [ 'query' => ['foo' => 'bar'] ]);,但这也不起作用:
$response = $client->request('POST', $endpoint, [
'auth' => 'oauth',
'form_params' => [
'price' => '21'
]
]);
这是我当前的代码,get $client->get() 注释掉了成功返回产品的内容。
$endpoint = 'products/12';
$handler = new \GuzzleHttp\Handler\CurlHandler();
$stack = \GuzzleHttp\HandlerStack::create($handler);
$middleware = new \GuzzleHttp\Subscriber\Oauth\Oauth1([
'consumer_key' => $this->consumer_key,
'consumer_secret' => $this->consumer_secret,
'token_secret' => '',
'token' => '',
'request_method' => Oauth1::REQUEST_METHOD_QUERY,
'signature_method' => Oauth1::SIGNATURE_METHOD_HMAC
]);
$stack->push($middleware);
$client = new \GuzzleHttp\Client([
'base_uri' => $this->url . $this->api,
'handler' => $stack
]);
$response = $client->post( $endpoint, [ 'auth' => 'oauth' ], ['price' => '21'] );
var_dump($response);
//$response = $client->get( $endpoint, [ 'auth' => 'oauth' ] );
//return array(
// 'status' => $response->getStatusCode(),
// 'header' => $response->getHeaderLine('content-type'),
// 'body' => json_decode($response->getBody())
//);
【问题讨论】:
-
您正在发送
POST请求,您应该发送PUT请求。POST表示“创建”,PUT表示“更新”。
标签: php symfony woocommerce guzzle woocommerce-rest-api