【发布时间】:2018-07-18 01:08:03
【问题描述】:
已解决 这适用于 ecomdash api,因为在使用此方法时,所有 json 字符串都必须括在方括号中,就好像有一个产品数组一样。因此,对于一系列产品,这实际上与 json_encode 一样有效,但是只有一个产品被更新,您必须手动在 json 周围添加方括号。因此,因此您在设置请求时必须使用 body 而不是 json。所以这是最终的工作代码。
$data[] = array("Sku" => "067567", "Quantity" => 21, "WarehouseId" => 28345);
$dataJson = json_encode($data);
//put square brackets in if only one product is being updated
if(count($data) == 1) {
$dataJson = '['.$dataJson.']';
}
$headers = $this->auth;
$headers['Content-Type'] = 'application/json';
$params = array('headers' => $headers,
'json' => $data);
$response = $this->client->request('POST', 'inventory/updateQuantityOnHand', array('headers' => $headers, 'body' => $dataJson));
var_dump(json_decode($response->getBody(), true));
die();
}
我到处搜索并尝试了一百万件事,但是在尝试使用 guzzle 将 json 作为 POST 请求发送时,我一直收到同样的错误。
我发送的标头只是我正在使用的 api 的凭据,它们正在处理其他请求,所以我不认为它们是问题。
这是我使用的一些代码。
$data = array("Sku" => "067567", "Quantity" => 10, "WarehouseId" =>
28345);
$dataJson = json_encode($data, JSON_FORCE_OBJECT);
$headers = $this->auth;
$headers['Content-Type'] = 'application/json';
$params = array('headers' => $headers,
'body' => $dataJson);
$response = $this->client-
>post('inventory/updateQuantityOnHand',$params);
$data = array("Sku" => "067567", "Quantity" => 10, "WarehouseId" =>
28345);
$headers = $this->auth;
$headers['Content-Type'] = 'application/json';
$params = array('headers' => $headers,
'json' => $data);
$response = $this->client-
>post('inventory/updateQuantityOnHand',$params);
我得到的错误是
致命错误:未捕获的 GuzzleHttp\Exception\ClientException:客户端错误:
POST https://ecomdash.azure-api.net/api/inventory/updateQuantityOnHand导致400 Bad Request响应:{"Message":"您的请求正文丢失或无效","ExceptionMessage":"The /Applications/MAMP/htdocs/sunglasses/libraries/vendor/guzzlehttp/guzzle/ 中的请求正文丢失或无效","ExceptionType":"System.InvalidOperationException","StackTrace":null} (截断...) src/Exception/RequestException.php:111 堆栈跟踪:#0 /Applications/MAMP/htdocs/sunglasses/libraries/vendor/guzzlehttp/guzzle/src/Middleware.php(65): GuzzleHttp\Exception\RequestException::create(Object (GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response)) #1 /Applications/MAMP/htdocs/sunglasses/libraries/vendor/guzzlehttp/promises/src/Promise.php(203): GuzzleHttp\Middleware:: GuzzleHttp{closure}(Object(GuzzleHttp\Psr7\Response)) #2 /Applications/MAMP/htdocs/sunglasses/libraries/vendor/guzzlehttp/promises/src/Promise.php(156):/Applications/MAMP 中的 GuzzleHttp\Promis /htdocs /sunglasses/libraries/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php 在第 111 行
来自调试标志的信息
> POST /api/inventory/updateQuantityOnHand HTTP/1.1
Host: ecomdash.azure-api.net
User-Agent: GuzzleHttp/6.2.1 curl/7.52.1 PHP/7.1.8
Content-Type: application/json
ecd-subscription-key: *****************
Ocp-Apim-Subscription-Key: ****************
Content-Length: 50
* upload completely sent off: 50 out of 50 bytes
< HTTP/1.1 400 Bad Request
< Cache-Control: no-cache
< Pragma: no-cache
< Content-Length: 199
< Content-Type: application/json; charset=utf-8
< Expires: -1
< Request-Context: appId=cid-v1:6baf8227-a96f-4757-ac3b-a170d33a7b16
< Set-Cookie: ARRAffinity=5b826c9996f848edeab28288985b46ca9013ce4aef93b8f195bc66f2f91c578c;Path=/;HttpOnly;Domain=ecomdashapi.azurewebsites.net
< X-AspNet-Version: 4.0.30319
< X-Powered-By: ASP.NET
< Date: Wed, 07 Feb 2018 17:02:00 GMT
<
* Curl_http_done: called premature == 0
* Connection #0 to host ecomdash.azure-api.net left intact
我使用的是 Guzzle 版本 6.2.1
在此先感谢您提供的任何帮助,这让我很生气。
【问题讨论】:
-
您是否同时尝试“两种”代码?
-
不只是把它们放在那里作为我尝试过的两个例子。两者都导致了同样的错误。
-
您可以传递
, ['debug' => true]参数进行调试 -
我将调试返回的信息添加到问题中
-
这很奇怪。你能告诉我们
$this->auth的结构吗?但请记住将任何密码或敏感信息替换为***