如何在PHP中的cURL POST HTTP请求中包括授权头? [英] How to include Authorization header in cURL POST HTTP Request in PHP?

查看:762
本文介绍了如何在PHP中的cURL POST HTTP请求中包括授权头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Gmails OAuth 2.0访问用户的电子邮件,而我正在通过Google的OAuth 2.0操场找到此信息。



指定我需要以HTTP REQUEST发送:

  POST / mail / feed / atom / HTTP / 1.1 
主机:mail.google.com
Content-length:0
Content-type:application / json
授权:OAuth SomeHugeOAuthaccess_tokenThatIReceivedAsAString

我已经尝试编写一个代码来发送这个REQUEST,例如:

  $ 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 [] ='授权: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 :) :) :)谢谢!

解决方案

@ jason-mccreary是完全正确的。此外,我建议你这个代码,以获得更多的信息,如果故障:

  $ rest = curl_exec 

if($ rest === false)
{
// throw new Exception('Curl error:'。curl_error($ crl));
print_r('Curl error:'。curl_error($ crl));
}

curl_close($ crl);
print_r($ rest);



编辑1



CURLOPT_HEADER 设置为true可使用 firebug :: net 或类似的。

  curl_setopt($ crl,CURLOPT_HEADER,true); 

编辑2



关于 Curl错误:SSL证书问题,验证CA证书是否尝试添加这个头(只是为了调试,在生产环境中,应该保留这些选项 true ):

  curl_setopt($ crl,CURLOPT_SSL_VERIFYHOST,false); 
curl_setopt($ crl,CURLOPT_SSL_VERIFYPEER,false);


I'm trying to access mails of a user through Gmails OAuth 2.0, and I'm figuring this out through Google's OAuth 2.0 Playground

Here, they've specified I need to send this as a HTTP REQUEST:

POST /mail/feed/atom/ HTTP/1.1
Host: mail.google.com
Content-length: 0
Content-type: application/json
Authorization: OAuth SomeHugeOAuthaccess_tokenThatIReceivedAsAString

I've tried writing a code to send this REQUEST like this:

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

Not working, please help. :)

UPDATE: I took Jason McCreary's advice and now my code looks like this:

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

But I'm not getting any output out of this. I think cURL is silently failing somewhere. Please do help. :)

UPDATE 2: NomikOS's trick did it for me. :) :) :) Thank you!!

解决方案

@jason-mccreary is totally right. Besides I recommend you this code to get more info in case of malfunction:

$rest = curl_exec($crl);

if ($rest === false)
{
    // throw new Exception('Curl error: ' . curl_error($crl));
    print_r('Curl error: ' . curl_error($crl));
}

curl_close($crl);
print_r($rest);

EDIT 1

To debug you can set CURLOPT_HEADER to true to check HTTP response with firebug::net or similar.

curl_setopt($crl, CURLOPT_HEADER, true);

EDIT 2

About Curl error: SSL certificate problem, verify that the CA cert is OK try adding this headers (just to debug, in a production enviroment you should keep these options in true):

curl_setopt($crl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($crl, CURLOPT_SSL_VERIFYPEER, false);

这篇关于如何在PHP中的cURL POST HTTP请求中包括授权头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆