将 Postman 请求转换为 guzzle 或其他 PHP HTTP 客户端
Posted
技术标签:
【中文标题】将 Postman 请求转换为 guzzle 或其他 PHP HTTP 客户端【英文标题】:Convert Postman request to guzzle or other PHP HTTP client 【发布时间】: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::GuzzleHttpclosure(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::GuzzleHttpclosure(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 客户端?
【问题讨论】:
请帮忙。谢谢 我遇到了同样的问题,你是怎么解决的? 【参考方案1】:您收到的 422 错误消息的截断部分是什么?可能是您缺少必需的变体 ID。使用 guzzle,这是对我有用的请求,改编自 https://***.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>';
【讨论】:
以上是关于将 Postman 请求转换为 guzzle 或其他 PHP HTTP 客户端的主要内容,如果未能解决你的问题,请参考以下文章
将 paypal curl 获取访问令牌请求转换为 guzzle
如何使用Curl将这个mailchimp API请求转换为使用PHP的Guzzle?