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

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

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Handling Authorization Headers in cURL for Gmail OAuth 2.0

When integrating with Gmail's IMAP or other Google services via OAuth 2.0, the protocol requires sending specific headers alongside your request. A common stumbling block occurs when attempting to replicate a curl example from the Google OAuth 2.0 Playground using PHP's cURL extension. The challenge isn't just about setting the header; it is about ensuring the connection is properly established and that the server accepts the empty body expected by the API.

Consider a scenario where you need to access a user's mail feed. The request target might look like this:

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

The critical detail here is the Content-length: 0. This indicates that the request body is intentionally empty, despite the Content-Type being set to application/json. If you attempt to send data in the body, or if you fail to correctly format the headers array, cURL will either reject the request or the server will respond with a 400 Bad Request.

A typical initial attempt in PHP might look like this:

$crl = curl_init();
$header[] = 'Content-length: 0'; 
$header[] = 'Content-type: application/json';

curl_setopt($crl, CURLOPT_HTTPHEADER, $header);
curl_setopt($crl, CURLOPT_POST,       true);
// This line is often the culprit for unexpected behavior with OAuth tokens
curl_setopt($crl, CURLOPT_POSTFIELDS, urlencode($accesstoken)); 

$rest = curl_exec($crl);
print_r($rest);

In this snippet, a developer might instinctively try to pass the access token via CURLOPT_POSTFIELDS. However, this is incorrect for an OAuth Bearer or OAuth 1.0a flow where the token belongs in the Authorization header, not the request body. Furthermore, passing data via CURLOPT_POSTFIELDS can sometimes cause cURL to ignore the explicit Content-length: 0 header you set, leading to a mismatch between what the client claims to send and what it actually sends.

The correct approach involves strictly managing the headers and ensuring no POST fields are sent if the API expects an empty payload. The code should be structured as follows:

$crl = curl_init();

$headr = array();
$headr[] = 'Content-length: 0';
$headr[] = 'Content-type: application/json';
// Ensure there are no spaces after the colon and values are properly concatenated
$headr[] = 'Authorization: OAuth ' . $accesstoken;

curl_setopt($crl, CURLOPT_HTTPHEADER, $headr);
curl_setopt($crl, CURLOPT_POST, true);
// Do not set CURLOPT_POSTFIELDS if the body must be empty
// curl_setopt($crl, CURLOPT_POSTFIELDS, ''); 

$rest = curl_exec($crl);
curl_close($crl);

print_r($rest);

The key adjustments here are removing any logic that populates CURLOPT_POSTFIELDS and ensuring the header string for Authorization is concatenated correctly. If you do not pass a body, cURL defaults to sending none, but explicitly setting Content-length: 0 in the header array reinforces this intent, which some Google APIs validate strictly.

If you find yourself needing to handle authentication logic more robustly without writing low-level HTTP requests from scratch, consider using Laravel's built-in request handling or its specific OAuth 2.0 integration features. For instance, Laravel provides a seamless way to manage OAuth 2.0 flows through packages like laravel/socialite, which abstracts away the manual header management required by raw cURL calls.

// Example using Laravel Socialite for Google OAuth
$user = Socialite::driver('google')->user();
// The token is handled internally, but you can access the token via:
$token = $user->token; 

While manual cURL gives you granular control over every header and option, frameworks like Laravel often handle the complexity of token retrieval and validation more gracefully. However, when dealing with specific IMAP feeds or older Google APIs that strictly require the Authorization: OAuth prefix (rather than Bearer), raw cURL remains a necessary tool.

The final step is always to check the return value of curl_exec. If it returns false, inspect curl_error($crl) and curl_errno($crl) to identify silent failures, such as SSL certificate verification issues or network timeouts. Once these are resolved, the request should successfully return the Atom feed data for the user's mail.

Our network