当前位置: 首页 > 知识库问答 >
问题:

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

籍永安
2023-03-14

我有邮递员要求W/C工作良好。

当我单击Send按钮时,它工作得很好,它返回正确的资源,然而,当我试图在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>";
$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不可处理实体响应:{“status”:422,“title”:“缺少或不正确的必需字段”,“type”:“https://developer.bigcommerce.com/api#api-status-Co(截断...)在zzleHTTP\guzzle\src\middleware.php(66):GUZZLEHTTP\Exception\RequestException::Create(Object(GUZZLEHTTP\PSR7\request),Object(GUZZLEHTTP\PSR7\response))#1C:\www

GUZZLEHTTP\Promise\Promise::CallHandler(1,Object(GUZZLEHTTP\psr7\response),Array)#3C:\www\bombon-shelect\http\vendor\guzzleHTTP\promes\srce\taskqueue.php(47):GUZZLEHTTP\prome\promi,在C:\www\bombon-shelect\http\vendor\guzzle\scr\exception\requestexception.php第113行

你知道我怎么才能让它在Guzzle上工作吗?还是其他PHP HTTP客户端?

共有1个答案

哈雅珺
2023-03-14

您得到的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>';
 类似资料:
  • 我试图隐藏一个卷曲来吞咽请求,这里是卷曲请求。 以下是JSON部分:

  • 我向我的flask应用所在的服务器发出了python post请求。它运行良好,我能够获得所需的数据。 但我想用POSTMAN测试API。我无法做到这一点,因为在某种程度上我对邮递员并不熟悉。 下面是我的python代码。 我正在努力解决这样一个问题:我试图发布到服务器上的数据和文件应该是原始json还是表单数据,或者是正文的x-www-form-urlencoded部分。还有实际的结构应该是什么

  • 在SWIFT中,我无法将以下curl转换为url请求。 我尝试了以下方法,但在解压缩一个可选值错误时意外地发现为nil 我尝试了这个链接中的解决方案,在Swift中将curl转换为URL请求

  • 所以我得到了这个简单的HTML表单 这允许我上传一个映像并将其发布到服务器上,在服务器上由“upload.php”脚本处理。 如下所示 如果可以在JavaScript中完全做到这一点,我很想知道。 周末愉快:)

  • 我有邮递员的API。我想创建一个CURL请求,并用它得到适当的响应。这是我的邮递员API。 我成功地用它得到了这个回应。 我使用了curl_error,它给出了以下错误。 Curl错误:SSL证书问题:证书链中的自签名证书

  • 问题内容: 我有以下CURL请求,谁能请我确认subesquest HTTP请求是什么? 会是这样吗? 任何人都可以帮助我将上述curl请求完全转换为httpreq。 提前致谢。 苏维 问题答案: 有很多方法可以实现这一目标。在我看来,以下一项是最简单的,同意它不是很灵活,但是可以工作。