【发布时间】:2018-10-30 17:40:39
【问题描述】:
我有邮递员请求 w/c 运行良好。
当我单击发送按钮时它运行良好,它返回正确的资源,但是,当我尝试在 PHP 中使用 guzzlehttp/guzzle 执行它时,它返回 422 或 400
我的代码
$res = $client->request('POST', 'https://api.bigcommerce.com/stores/ny813bjwdy/v3/carts', [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'X-Auth-Client' => "mzr2qe4qeweqwe",
'X-Auth-Token' => "nokrq2131qweqrqrqew"
],
'form_params' => [
'customer_id' => 1,
'line_items' => [
'quantity' => 1,
'product_id' => 97,
'list_price' => 200
]
]
]);
echo "<pre>";
print_r($res);
echo "</pre>";
致命错误:未捕获的 GuzzleHttp\Exception\ClientException:客户端 错误:
POST https://api.bigcommerce.com/stores/ny813bjwdy/v3/carts导致400 Bad Request响应:{"status":400,"title":"Input 是 无效","type":"https://developer.bigcommerce.com/api#api-status-codes","detail":"语法 (截断...)在 C:\www\bomb-shelter\http\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php:113 堆栈跟踪:#0 C:\www\bomb-shelter\http\vendor\guzzlehttp\guzzle\src\Middleware.php(66): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), 对象(GuzzleHttp\Psr7\Response))#1 C:\www\bomb-shelter\http\vendor\guzzlehttp\promises\src\Promise.php(203): GuzzleHttp\Middleware::GuzzleHttp{closure}(Object(GuzzleHttp\Psr7\Response))2 C:\www\bomb-shelter\http\vendor\guzzlehttp\promises\src\Promise.php(156):
GuzzleHttp\Promise\Promise::callHandler(1, 对象(GuzzleHttp\Psr7\Response),数组)#3 C:\www\bomb-shelter\http\vendor\guzzlehttp\promises\src\TaskQueue.php(47): GuzzleHttp\Promise\Promise::Gazzl in C:\www\bomb-shelter\http\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php 在第 113 行
我也试过 GuzzleHttp\RequestOptions::JSON
$res = $client->request('POST', 'https://api.bigcommerce.com/stores/ny813bjwdy/v3/carts', [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'X-Auth-Client' => "mzr2qe4qeweqwe",
'X-Auth-Token' => "nokrq2131qweqrqrqew"
],
GuzzleHttp\RequestOptions::JSON => [
'customer_id' => 1,
'line_items' => [
'quantity' => 1,
'product_id' => 97,
'list_price' => 200
]
]
]);
echo "<pre>";
print_r($res);
echo "</pre>";
但它返回 422
致命错误:未捕获的 GuzzleHttp\Exception\ClientException:客户端 错误:
POST https://api.bigcommerce.com/stores/ny813bjwdy/v3/carts导致422 Unprocessable Entity响应: {"status":422,"title":"缺少或不正确的必填项 字段","类型":"https://developer.bigcommerce.com/api#api-status-co (截断...)在 C:\www\bomb-shelter\http\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php:113 堆栈跟踪:#0 C:\www\bomb-shelter\http\vendor\guzzlehttp\guzzle\src\Middleware.php(66): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), 对象(GuzzleHttp\Psr7\Response))#1 C:\www\bomb-shelter\http\vendor\guzzlehttp\promises\src\Promise.php(203): GuzzleHttp\Middleware::GuzzleHttp{closure}(Object(GuzzleHttp\Psr7\Response))2 C:\www\bomb-shelter\http\vendor\guzzlehttp\promises\src\Promise.php(156):
GuzzleHttp\Promise\Promise::callHandler(1, 对象(GuzzleHttp\Psr7\Response),数组)#3 C:\www\bomb-shelter\http\vendor\guzzlehttp\promises\src\TaskQueue.php(47): GuzzleHttp\Promise\Promi 在 C:\www\bomb-shelter\http\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php 在第 113 行
知道如何让它在 guzzle 上工作吗?还是其他 PHP HTTP 客户端?
【问题讨论】:
-
请帮忙。谢谢
-
我遇到了同样的问题,你是怎么解决的?
标签: php postman bigcommerce guzzle6 guzzle