How to include Authorization header in cURL POST HTTP Request in PHP?

PhpHttpCurlOauth

Php Problem Overview


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!!

Php Solutions


Solution 1 - Php

You have most of the code…

CURLOPT_HTTPHEADER for curl_setopt() takes an array with each header as an element. You have one element with multiple headers.

You also need to add the Authorization header to your $header array.

$header = array();
$header[] = 'Content-length: 0';
$header[] = 'Content-type: application/json';
$header[] = 'Authorization: OAuth SomeHugeOAuthaccess_tokenThatIReceivedAsAString';

Solution 2 - Php

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

Solution 3 - Php

use "Content-type: application/x-www-form-urlencoded" instead of "application/json"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1437671View Question on Stackoverflow
Solution 1 - PhpJason McCrearyView Answer on Stackoverflow
Solution 2 - PhpIgor ParraView Answer on Stackoverflow
Solution 3 - PhpAnudeep SharmaView Answer on Stackoverflow