gpt4 book ai didi

php - 如何使用 Guzzle 更新 Woocommerce Order API

转载 作者:可可西里 更新时间:2023-11-01 13:25:39 27 4
gpt4 key购买 nike

我可以使用 guzzle 获取订单详细信息。但我无法更新订单。

这是我的代码:

use stdClass;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;


$data = new stdClass();
$data->fulfillment = new stdClass();

$trackingUrl = "123456789";

$shopUrl = "localhost/Test";
$consumerKey = "cs_mykey";
$consumerSecret = "ck_mykey";
$orderId = "123";

$subPath = "/wc-api/v2/orders/".$orderId;

$data->fulfillment->tracking_url = $trackingUrl;
$data->fulfillment->status = 'completed';

$headers = array(
'Content-Type: application/json'
);

$method = "POST";

$url = "http://localhost/Test/wc-api/v2/orders/123?oauth_consumer_key=ck_mykey&consumer_key=ck_mykey&consumer_secret=cs_mykey&oauth_timestamp=1505544895&oauth_nonce=9ecd49e80860e09ddaf91f148451532620976b8d&oauth_signature_method=HMAC-SHA256&oauth_signature=mysignature";

$Result = callApi($url, json_encode($data), $headers, $method);

echo '<pre>'; print_r($Result);


function callApi($url = NULL, $body = NULL, $headers = array(), $requestType = "POST")
{
$client = new GuzzleHttp\Client();
$body = $body ? $body : new stdClass();
$request = $client->POST($url)->setPostField($body)->send();

$data = $request->getBody()->getContents();
return json_decode($data);

}

使用上面的代码我会得到如下错误

导致 400 Bad Request 响应:{"errors":[{"code":"woocommerce_api_missing_callback_param","message":"Missing parameter data"}]} ' 在 C:\xampp\htdocs\Guzzle\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php:113 堆栈跟踪:#0 C:\xampp\htdocs\Guzzle\vendor\guzzlehttp\guzzle\src\Middleware.php(65): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response)) #1 C:\xampp\htdocs\Guzzle\vendor\guzzlehttp\promises 复制代码\src\Promise.php(203): 在C:\xampp\htdocs\Guzzle\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php 113行

我不知道我在上面错过了什么。帮我整理一下。

谢谢。

最佳答案

在我更改了以下功能后,现在顺序已更新。

function callApiPost($url = NULL, $body = NULL, $headers = array(), $requestType = "POST")
{

$client = new Client();
$body = $body ? $body : new stdClass();
$request = new Request($requestType, $url, $headers, json_encode($body));
$response = $client->send($request, ['timeout' => 10]);
if($requestType === 'DELETE') {
return $httpCode = $response->getStatusCode();
} else {
$data = $response->getBody()->getContents();
return json_decode($data);
}
}

通过使用 guzzle,我们不需要使用 Post 函数,我们只需获取请求并发送请求,它就会更新订单。

一个小小的改变..

关于php - 如何使用 Guzzle 更新 Woocommerce Order API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46251547/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com