如何在 PHP 中的 cURL POST HTTP 请求中包含 Authorization 标头?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12331224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to include Authorization header in cURL POST HTTP Request in PHP?
提问by
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
我正在尝试通过 Gmails OAuth 2.0 访问用户的邮件,我正在通过 Google 的 OAuth 2.0 Playground 解决这个问题
Here, they've specified I need to send this as a HTTP REQUEST:
在这里,他们指定我需要将此作为 HTTP 请求发送:
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:
更新:我接受了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);
But I'm not getting any output out of this. I think cURL is silently failing somewhere. Please do help. :)
但我没有得到任何输出。我认为 cURL 在某个地方默默地失败了。请帮忙。:)
UPDATE 2:NomikOS's trick did it for me. :) :) :) Thank you!!
更新 2:NomikOS的技巧为我做到了。:) :) :) 谢谢!!
采纳答案by Igor Parra
@jason-mccreary is totally right. Besides I recommend you this code to get more info in case of malfunction:
@jason-mccreary 是完全正确的。此外,我建议您使用此代码以在出现故障时获取更多信息:
$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
编辑 1
To debug you can set CURLOPT_HEADERto true to check HTTP response with firebug::netor similar.
要调试,您可以设置CURLOPT_HEADER为 true 以使用firebug::net或类似方法检查 HTTP 响应。
curl_setopt($crl, CURLOPT_HEADER, true);
EDIT 2
编辑 2
About Curl error: SSL certificate problem, verify that the CA cert is OKtry adding this headers (just to debug, in a production enviroment you should keep these options in true):
关于Curl error: SSL certificate problem, verify that the CA cert is OK尝试添加此标头(只是为了调试,在生产环境中您应该将这些选项保留在true):
curl_setopt($crl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($crl, CURLOPT_SSL_VERIFYPEER, false);
回答by Jason McCreary
You have most of the code…
您拥有大部分代码……
CURLOPT_HTTPHEADERfor curl_setopt()takes an array with each header as an element. You have one element with multiple headers.
CURLOPT_HTTPHEADERforcurl_setopt()接受一个数组,每个标题作为一个元素。您有一个带有多个标题的元素。
You also need to add the Authorizationheader to your $headerarray.
您还需要将Authorization标头添加到您的$header数组中。
$header = array();
$header[] = 'Content-length: 0';
$header[] = 'Content-type: application/json';
$header[] = 'Authorization: OAuth SomeHugeOAuthaccess_tokenThatIReceivedAsAString';
回答by Anudeep Sharma
use "Content-type: application/x-www-form-urlencoded" instead of "application/json"
使用“内容类型:应用程序/x-www-form-urlencoded”而不是“应用程序/json”

