对于第一步要进行身份验证的cURL请求,我具有以下代码:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.example.com/v1/api2/login?grant_type=client_id",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic abcdefhjijkjljpeqrperpqweprwqprqrp",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
$response = json_decode($response);
此代码可以正常工作并返回Bearer令牌对象。
更新:这是第一个cURL请求的响应:
{"access_token":"afasfadsfaewrererearaere34343434-34-3403-403-403-3-3434343434343","type":"Bearer"}
现在,在获得Bearer令牌后,我想使用相同的API发出GET请求,这是我遇到问题的地方:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://api.example.com/v1/api2/event/adfafsafsfafsffs",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer " . $response['access_token'],
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
当我尝试运行该代码时,该代码超时。
以下是格式化请求的文档:
curl -X GET \
http://api.example.com/v1/api2/event/adfafsafsfafsffs \
-H 'Authorization: Bearer bzJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9'
如何格式化第二个cURL请求,使其与上面的说明兼容,并且在身份验证后可以发出GET请求?
感谢您的帮助!