简体   繁体   English

在curl通话期间发送标题

[英]Send Headers during a curl call

Hi I have to send a request to an api(pitney bowls geolocation) for which I need an access token from a site. 嗨,我必须向api(盆碗地理定位)发送请求,我需要从该站点获取访问令牌。 In the website its mentioned as - To get an access token, set header and request body and call the token URI: 在网站中,它被称为-要获取访问令牌,请设置标头和请求正文,然后调用令牌URI:

Authorization: Basic {BASE64 ENCODED VALUE} 
Content-Type: application/x-www-form-urlencoded 
POST https://api.pitneybowes.com/oauth/token
grant_type=client_credentials 

I am currently using the following code : - 我目前正在使用以下代码:-

$val= base64_encode ('{gR6Ov7fCmuzHcVXE6bsmcUOt3SXhmXlL}:{ud61in4FYSGyF0eU}');
 $ch = curl_init();
$headr = array();
$headr[] = 'Authorization: Basic {'.$val.'}';
$headr[] = 'Content-Type: application/x-www-form-urlencoded';
$headr[]='grant_type=client_credentials';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headr);
curl_setopt($ch, CURLOPT_URL, "https://api.pitneybowes.com/oauth/token");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);

curl_close($ch);

However am not getting any result. 但是没有任何结果。 Any idea what might be wrong? 知道有什么问题吗?

The third header you are adding, is not a valid header. 您要添加的第三个标头不是有效的标头。

You should probably add that as a POST key-value pair: 您可能应该将其添加为POST键值对:

curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM