如何在PHP的cURL POST HTTP请求中包含授权标头?
我正在尝试通过Gmails OAuth 2.0访问用户的邮件,并且我正在通过Google的OAuth 2.0 Playground来解决这个问题。
在这里,他们指定我需要将其作为HTTP请求发送:
POST /mail/feed/atom/ HTTP/1.1
Host: mail.google.com
Content-length: 0
Content-type: application/json
Authorization: OAuth SomeHugeOAuthaccess_tokenThatIReceivedAsAString
我尝试编写一个代码来发送此请求,如下所示:
$crl = curl_init();
$header[] = 'Content-length: 0
Content-type: application/json';
curl_setopt($crl, CURLOPT_HTTPHEADER, $header);
curl_setopt($crl, CURLOPT_POST, true);
curl_setopt($crl, CURLOPT_POSTFIELDS, urlencode($accesstoken));
$rest = curl_exec($crl);
print_r($rest);
不工作,请帮忙。:)
更新:我接受了Jason McCreary的建议,现在我的代码看起来像这样:
$crl = curl_init();
$headr = array();
$headr[] = 'Content-length: 0';
$headr[] = 'Content-type: application/json';
$headr[] = 'Authorization: OAuth '.$accesstoken;
curl_setopt($crl, CURLOPT_HTTPHEADER,$headr);
curl_setopt($crl, CURLOPT_POST,true);
$rest = curl_exec($crl);
curl_close($crl);
print_r($rest);
但是我没有从中得到任何输出。我认为cURL在某个地方默默地失败了。请帮忙。:)
更新2:NomikOS的技巧为我做到了。:) :) :)谢谢!!