首页 文章

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

提问于
浏览
0

我有邮递员要求w / c运作良好 .

以下是我的帖子网址和 Headers
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>";

致命错误:未捕获GuzzleHttp \ Exception \ ClientException:客户端错误:POST https://api.bigcommerce.com/stores/ny813bjwdy/v3/carts导致400 Bad Request响应:{“status”:400,“title” :“输入无效”,“类型”:“https://developer.bigcommerce.com/api#api-status-codes”,“detail”:“C:\ www \ bomb中的Synta(截断...) -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),Object(GuzzleHttp \ Psr7 \ Response))#1 C:\ www \ bomb-shelter \ http \ vendor \ guzzlehttp \ promises \ src \ Promise .php(203):GuzzleHttp \ Middleware :: GuzzleHttp (对象(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),数组)#3 C:\ www \ bomb-shelter \ http \ vendor \ guzzlehttp \ promises \ src \ TaskQueue.php(47 ): G uzzleHttp \ Promise \ Promise :: Guzzl在第113行的C:\ www \ bomb-shelter \ http \ vendor \ guzzlehttp \ guzzle \ src \ Exception \ RequestException.php

我也尝试过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无法处理的实体响应:{“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),Object(GuzzleHttp \ Psr7 \ Response))#1 C:\ www \ bomb-shelter \ http \ vendor \ guzzlehttp \ promises \ src \ Promise.php(203): GuzzleHttp \ Middleware :: GuzzleHttp (对象(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.p hp(47):第113行的C:\ www \ bomb-shelter \ http \ vendor \ guzzlehttp \ guzzle \ src \ Exception \ RequestException.php中的GuzzleHttp \ Promise \ Promi

任何想法我怎么能让它在guzzle上工作?或其他PHP HTTP客户端?

1 回答

  • 0

    422错误消息的截断部分是什么're getting? It might be that you'缺少必需的变体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>';
    

相关问题