我有一个可以使用的 curl 片段,但是 Guzzle 出现 400 错误,为什么?

谁能看到我在这里可能做错了什么?Curl 有效,但 Guzzle (v6.3) 出现 400 错误。


Curl 由 Postman 生成:


卷曲:


    $curl = curl_init();

    curl_setopt_array($curl, array(

        CURLOPT_URL => "https://unnamed-website.com/api2/v2/charges?chargeId=273628584&paymentGate=inovio",

        CURLOPT_RETURNTRANSFER => true,

        CURLOPT_ENCODING => "",

        CURLOPT_MAXREDIRS => 10,

        CURLOPT_TIMEOUT => 0,

        CURLOPT_FOLLOWLOCATION => true,

        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,

        CURLOPT_CUSTOMREQUEST => "GET",

        CURLOPT_HTTPHEADER => array(

            "app-token: toktoktoktoktotkoktoktokt",

            "Content-type: application/json",

            "Authorization: Basic MDFkNWJiMTllYTdlZjVjODYzYTVjYjI3ZjUzNWY4NmM6YzFhMzRlNWJkZGYyMzljZTFmZDcwZjNiZDk0Y2Q4ZjA="

        ),

    ));

    $response = curl_exec($curl);

输出:



大嘴:


$guzzle = new Client();

$request = $guzzle->get("https://unnamed-website.com/api2/v2/charges?chargeId=273628584&paymentGate=inovio",

[

    'headers' => [

        "app-token" => "toktoktoktoktotkoktoktokt",

        "Content-type" => "application/json",

        "Authorization" => "Basic MDFkNWJiMTllYTdlZjVjODYzYTVjYjI3ZjUzNWY4NmM6YzFhMzRlNWJkZGYyMzljZTFmZDcwZjNiZDk0Y2Q4ZjA="

    ]

]);

$response = $request->send();

输出:


   Client error response

   [status code] 400

   [reason phrase] Bad Request



catspeake
浏览 211回答 2
2回答

呼如林

Guzzle 可以这样调用:<?php&nbsp; &nbsp; $client = new GuzzleHttp\Client();&nbsp; &nbsp; $response = $client->request('GET',"https://unnamed-website.com/api2/v2/charges?chargeId=273628584&paymentGate=inovio",[&nbsp; &nbsp; &nbsp; &nbsp; 'headers' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "app-token" => "toktoktoktoktotkoktoktokt",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Content-type" => "application/json",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Authorization" => "Basic MDFkNWJiMTllYTdlZjVjODYzYTVjYjI3ZjUzNWY4NmM6YzFhMzRlNWJkZGYyMzljZTFmZDcwZjNiZDk0Y2Q4ZjA="&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; ]);&nbsp; &nbsp; $response = $request->send();&nbsp; &nbsp; $result = json_decode($response->getBody()->getContents());?>

12345678_0001

当我在 slack 上发布它时,我的开发人员同事们对此进行了深入研究,他们发现了问题。我在&nbsp;use Guzzle\Http\Client;应该使用的时候使用use GuzzleHttp\Client;...前者是可用的,因为在一个巨大的专有框架内有许多不受管理的供应商包。
打开App,查看更多内容
随时随地看视频慕课网APP