[英]php how to use bearer token in curl get request header?
below is a working c# get request下面是一个有效的 c# get 请求
public HttpResponseMessage ExecuteEndPoint(string endpoint,string accessTocken) {
HttpResponseMessage responseMessage = new HttpResponseMessage();
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessTocken);
responseMessage = client.GetAsync(endpoint).Result;
}
return responseMessage;
}
I would like to do the same request in php using curl, below is what i have tried我想在 php 使用 curl 做同样的请求,下面是我试过的
$request_headers=array();
$request_headers[]='Bearer: '.$access_token;
//$request_headers[]='Content-Length:150';
$handle1=curl_init();
$api_url='here my api get url';
curl_setopt_array(
$handle1,
array(
CURLOPT_URL=>$api_url,
CURLOPT_POST=>false,
CURLOPT_RETURNTRANSFER=>true,
CURLOPT_HTTPHEADER=>$request_headers,
CURLOPT_SSL_VERIFYPEER=>false,
CURLOPT_HEADER=>true,
CURLOPT_TIMEOUT=>-1
)
);
$data=curl_exec($handle1);
echo serialize($data);
Looks like the api is not receiving access_token.看起来 api 没有收到 access_token。 Any help is appreciated.
任何帮助表示赞赏。 Thanks
谢谢
You also need to specify "Authorization"...您还需要指定“授权”...
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: Bearer ".$access_token
));
From PHP 7 you have to use those options:从 PHP 7 你必须使用这些选项:
$curl = curl_init();
$options = [
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_HTTPAUTH => CURLAUTH_BEARER,
CURLOPT_XOAUTH2_BEARER => $token,
];
curl_setopt_array($curl, $options);
curl_exec($curl);
$request_headers[] = 'Bearer: ' . $request_headers[] = '承载:' 。 $access_token;, it seems like a typo ->
$access_token;,好像打错了->
$request_headers[] = 'Authorization: Bearer ' . $request_headers[] = '授权:不记名'。 $access_token;
$access_token; -> correct one
->纠正一个
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.