在卷曲呼叫期间发送标题

时间:2016-02-06 16:58:25

标签: php html curl

您好我必须向api(pitney bowls geolocation)发送请求,我需要从网站获取访问令牌。在其提到的网站中 - 要获取访问令牌,请设置标头和请求正文并调用令牌URI:

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

我目前正在使用以下代码: -

$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);

但是没有得到任何结果。知道什么可能是错的吗?

1 个答案:

答案 0 :(得分:1)

您要添加的第三个标头不是有效标头。

您应该将其添加为POST键值对:

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