本文介绍了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'
));

这篇关于401在curl PHP中发送请求时未经授权的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-20 23:17