我有一个简单的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的行为有所不同。
在邮递员中使用此网址时,我收到以下json响应:
{
"errorCode": 0,
"errorMessage": null,
"envelopeId": 140,
"blockchainAddress": "0xA300991F77519B5f30552F74779eeEdE398fA59e",
"clientData": "",
"signatureVerified": true
}
邮递员中我唯一要设置的是授权承载令牌,该令牌在我的cUrl请求中设置为相同的承载令牌。但是,我在这篇文章中没有使用我的真实令牌或api网址。
如果我做
var_dump($response);
die();
我在屏幕上打印的全部是
string(800)“ HTTP状态400 –错误的请求
答案 0 :(得分:0)
找出问题所在。仅仅是因为我要插入到'validate_url'字符串中的一个变量未进行urlencode编码,并且由于该变量的一部分包含'/'字符,因此被解释为错误的请求。