cUrl PHP Get请求返回400错误请求,但可在Postman中使用

时间:2020-01-23 05:05:06

标签: php curl

我有一个简单的API调用,该API接受url中的参数并在标头中发送承载令牌。我已经打印出网址,并使用已应用的承载令牌将其粘贴到Postman中,并且可以正常工作,但是在PHP中,我一直收到400 Bad Request错误。这是代码:

    $ch = curl_init();
    $authorization = 'Authorization: Bearer oireuoijgosrhj9r9regjoregojgjoislgfjs';
    $validate_url = 'https://api.mycustomapi.com/api/envelopes/validate/' . $auth_id .'/' . $content_hash . '/' . $envelope_id;
    curl_setopt($ch, CURLOPT_URL, $validate_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array($authorization ));
    $response = curl_exec($ch);
    $json = json_decode($response, true);
    $sigVer = $json['signatureVerified'];
    // Check HTTP status code
    if (!curl_errno($ch)) {
        switch ($http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE)) {
            case 200:  # OK
                echo 'response is: ', $sigVer, "\n";
                break;
            case 502:  # server error
                system_message('Received 500 Error from Server');
                break;
            case 400:  # bad request
                echo 'url is: ', $validate_url, "\n";
                system_message('Simple validation must have exactly one content item');
                break;
            default:
                 echo 'Unexpected HTTP code: ', $validate_url, "\n";
        }
    }
    curl_close($ch);

在邮递员中,我得到了预期的响应...不知道为什么cUrl Php的行为有所不同。

'validate_url'的计算结果为: https://api.myapiservice.com/api/envelopes/validate/auth0|5e17f7e988b0e90e9bdf2e12/6767b1944f523e790c8badf608de140599a424d174a2cad87859a7093aba039f7dc2baaab9f114bf371828364636ec654e1be6aff55e7f65140e24bbbf1a3a7e/140

在邮递员中使用此网址时,我收到以下json响应:

{
    "errorCode": 0,
    "errorMessage": null,
    "envelopeId": 140,
    "blockchainAddress": "0xA300991F77519B5f30552F74779eeEdE398fA59e",
    "clientData": "",
    "signatureVerified": true
}

邮递员中我唯一要设置的是授权承载令牌,该令牌在我的cUrl请求中设置为相同的承载令牌。但是,我在这篇文章中没有使用我的真实令牌或api网址。

如果我做

var_dump($response);
die();

我在屏幕上打印的全部是

string(800)“ HTTP状态400 –错误的请求

1 个答案:

答案 0 :(得分:0)

找出问题所在。仅仅是因为我要插入到'validate_url'字符串中的一个变量未进行urlencode编码,并且由于该变量的一部分包含'/'字符,因此被解释为错误的请求。