【问题标题】:API request works with cURL but not with Guzzle?API 请求适用于 cURL 但不适用于 Guzzle?
【发布时间】:2021-09-06 17:11:05
【问题描述】:

我正在尝试让这个请求正常工作,这是我第一次与 Guzzle 合作,我不知道我在这里做错了什么。该请求适用于 cURL,但不适用于 Guzzle:

大吃一惊:

$res = $client->request('POST', 'https://api.bol.com/retailer/offers', [
            'headers' => [
                'Authorization' => 'Bearer ' . $jwt,
                'Accept' => 'application/vnd.retailer.v5+json',
                'content-type' => 'application/vnd.retailer.v5+json'
            ],
            'body' => json_encode(array(
                'ean' => (string)$item->ean_number,
                'condition' => [
                    'name' => 'NEW',
                ],
                'reference' => 'NS-'.(string)$item->ean_number,
                'unknownProductTitle' => (string)$item->name,
                'pricing' => [
                    'bundlePrices' =>[
                        'quantity' => 1,
                        'unitprice' => round($item->default_price->price * 1.21, 2)
                    ],
                ],
                'stock' => [
                    'amount' => (int)$item->stock,
                    'managedByRetailer' => false,
                ],
                'fulfilment' => [
                    'method' => 'FBR',
                    'deliveryCode' => '2-3d',
                ]
            ))
        ]);

卷曲:

$productjson[] = array(
          'ean' => $data[4],
          'condition' => array('name' => 'NEW', 'category' => 'NEW'),
          'onHoldByRetailer' => 'false',
          'reference' => "dy".$data[4],
          'unknownProductTitle' => $data[1],
          'pricing' => array('bundlePrices' => array('quantity' => 1, 'unitPrice' => $data[12])),
          'stock'=> array('amount' => $data[13], 'managedByRetailer' => false),
          'fulfilment' => array('method' => 'FBR', 'deliveryCode' => '2-3d')
        );
        $productjson = json_encode($productjson[0], JSON_FORCE_OBJECT);
        $response = callAPI('POST', $bearer, 'https://api.bol.com/retailer/offers', $productjson);
function callAPI($method, $bearer, $url, $data, $accept = 'application/vnd.retailer.v4+json', $ignoreheaders = false){
  $curl = curl_init();

  if(!$ignoreheaders){
  
  curl_setopt($curl, CURLOPT_HEADER, true);
  }
  switch ($method){
     case "POST":
        curl_setopt($curl, CURLOPT_POST, 1);
        if ($data)
           curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        break;
     case "PUT":
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
        if ($data)
           curl_setopt($curl, CURLOPT_POSTFIELDS, $data);                               
        break;
        case "DELETE":
          curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
          if ($data)
             curl_setopt($curl, CURLOPT_POSTFIELDS, $data);                             
          break;
     default:
        if ($data)
           $url = sprintf("%s?%s", $url, http_build_query($data));
  }
  // OPTIONS:
  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: '.$accept, 'Content-Type: '.$accept,
  'Authorization: Bearer '.$bearer));
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  // EXECUTE:
  $result = curl_exec($curl);
  if(!$ignoreheaders){
  if (strpos($result, 'JWT') !== false) {
    $responses[] = 'Epired JWT:'.$result;
    $bearer = callBearer('POST', 'https://login.bol.com/token?grant_type=client_credentials', NULL);
    $result = curl_exec($curl);
  }
  if(strpos($result, '504') !== false){
    $responses[] = '504 error:'.$result;
    sleep(1.1);
    $bearer = callBearer('POST', 'https://login.bol.com/token?grant_type=client_credentials', NULL);
    $result = curl_exec($curl);
  }
  if(strpos($result, 'FAILURE') !== false){
    $responses[] = 'FAILURE:'.$result;
    sleep(1.1);
    $bearer = callBearer('POST', 'https://login.bol.com/token?grant_type=client_credentials', NULL);
    $result = curl_exec($curl);
  }
  $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
  $header = substr($result, 0, $header_size);
  $body = substr($result, $header_size);

  $result = json_decode($body);
  return $result;
}

这可能真的很愚蠢,但我基本上只是在问:我如何使用 guzzle 执行工作 cURL 请求?因为我的 guzzle 请求返回 400 bad request 错误。

【问题讨论】:

  • 尝试用 json 替换 body 并将数组而不是 json 编码字符串传递给它
  • @Haridarshan 嗨!感谢您的评论,我试过了,但很遗憾没有奏效。
  • 您能否通过在请求中启用'debug' => true 来分享完整的响应
  • curl 一起使用的有效负载与您在 guzzle 正文中发送的有效负载不同。尝试通过 guzzle 发送相同的有效载荷
  • @Haridarshan 没关系,你的第一反应是对的!你是英雄,谢谢。如果您发表评论作为答案,我会将其标记为官方答案!

标签: php json api curl guzzle


【解决方案1】:

尝试将 body 更改为 json 并传递数组而不是 json 编码字符串,Guzzle 会在内部将此数组转换为 json

$res = $client->request('POST', 'https://api.bol.com/retailer/offers', [
    'headers' => [
        'Authorization' => 'Bearer ' . $jwt,
        'Accept' => 'application/vnd.retailer.v5+json',
        'content-type' => 'application/vnd.retailer.v5+json'
    ],
    'json' => array(
        'ean' => (string)$item->ean_number,
        'condition' => [
            'name' => 'NEW',
        ],
        'reference' => 'NS-'.(string)$item->ean_number,
        'unknownProductTitle' => (string)$item->name,
        'pricing' => [
            'bundlePrices' =>[
                'quantity' => 1,
                'unitprice' => round($item->default_price->price * 1.21, 2)
            ],
        ],
        'stock' => [
            'amount' => (int)$item->stock,
            'managedByRetailer' => false,
        ],
        'fulfilment' => [
            'method' => 'FBR',
            'deliveryCode' => '2-3d',
        ]
    )
]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-06
    • 1970-01-01
    • 2020-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-08
    • 1970-01-01
    相关资源
    最近更新 更多