401在curl PHP中发送请求时未经授权的响应
我正在尝试使用GitLab API解决我的GitLab项目问题,我已经尝试过了
但似乎不起作用
<?php
$url = "https://git.exemple.com/api/v4/issues";
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_HTTPAUTH,CURLAUTH_ANY);
curl_setopt($ch,CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json',
'Authorization: PRIVATE-TOKEN xxxxxxxxxxxxxxxx'
));
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$resp = curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($e = curl_error($ch)){
echo $e;
}
else{
$decoded = json_decode($resp,true);
print_r($decoded);
}
curl_close($ch);
这是我收到的回复
Array ( [message] => 401 Unauthorized )
解决方案
简单阅读手册
Personal/project access tokens
建议您的标题应为:
curl_setopt($ch,CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json',
'PRIVATE-TOKEN: xxxxxxxxxxxxxxxx'
));
或
curl_setopt($ch,CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json',
'Authorization: Bearer xxxxxxxxxxxxxxxx'
));
相关文章