gpt4 book ai didi

php - 将 Postman 请求转换为 guzzle 或其他 PHP HTTP 客户端

转载 作者:行者123 更新时间:2023-12-05 07:33:42 24 4
gpt4 key购买 nike

我有 postman 请求 w/c 工作正常。

以下是我的帖子网址和标题 enter image description here

然后这是我的内容正文 enter image description here

当我单击“发送”按钮时它运行良好,它返回正确的资源,但是,当我尝试在 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>";

Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: POST https://api.bigcommerce.com/stores/ny813bjwdy/v3/carts resulted in a 400 Bad Request response: {"status":400,"title":"Input is invalid","type":"https://developer.bigcommerce.com/api#api-status-codes","detail":"Synta (truncated...) in C:\www\bomb-shelter\http\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php:113 Stack trace: #0 C:\www\bomb-shelter\http\vendor\guzzlehttp\guzzle\src\Middleware.php(66): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(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, Object(GuzzleHttp\Psr7\Response), Array) #3 C:\www\bomb-shelter\http\vendor\guzzlehttp\promises\src\TaskQueue.php(47): GuzzleHttp\Promise\Promise::Guzzl in C:\www\bomb-shelter\http\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php on line 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

Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: POST https://api.bigcommerce.com/stores/ny813bjwdy/v3/carts resulted in a 422 Unprocessable Entity response: {"status":422,"title":"Missing or incorrect required fields","type":"https://developer.bigcommerce.com/api#api-status-co (truncated...) in C:\www\bomb-shelter\http\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php:113 Stack trace: #0 C:\www\bomb-shelter\http\vendor\guzzlehttp\guzzle\src\Middleware.php(66): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(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, Object(GuzzleHttp\Psr7\Response), Array) #3 C:\www\bomb-shelter\http\vendor\guzzlehttp\promises\src\TaskQueue.php(47): GuzzleHttp\Promise\Promi in C:\www\bomb-shelter\http\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php on line 113

知道如何让它在 guzzle 上运行吗?还是其他 PHP HTTP 客户端?

最佳答案

您收到的 422 错误消息的截断部分是什么?可能是您缺少必需的变体 ID。使用 guzzle,这是对我有用的请求,改编自 https://stackoverflow.com/a/39525059/8521556 :

<?php
require 'vendor/autoload.php';

$cart = array(
'customer_id' => 1,
'line_items' => array(
array('quantity' => 1, 'product_id' => 1116, 'variant_id' => 1530)
),
);

json_encode($cart);

$client = new GuzzleHttp\Client([
'headers' => [
'Accept' => 'application/json',
'Content-type' => 'application/json',
'X-Auth-Client' => 'xxxxxxxxxxxx',
'X-Auth-Token' => 'xxxxxxxxxxxx' ]
]);

$response = $client->post('https://api.bigcommerce.com/stores/xxxxxxxx/v3/carts',
['json' => $cart]
);

echo '<pre>' . var_export($response->getStatusCode(), true) . '</pre>';
echo '<pre>' . var_export($response->getBody()->getContents(), true) . '</pre>';

关于php - 将 Postman 请求转换为 guzzle 或其他 PHP HTTP 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50442516/

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