The PHP cURL is a library used for making HTTP requests. cURL is a tool to transfer data from or to a server, using one of the supported protocols (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP etc..). In order to use PHP cURL, you must have installed and enabled libcurl module for PHP on your system. In this tutorial, we will learn about how to Post data with Authorization Headers using PHP cURL.

The HTTP Authorization request Header contains the credentials to authenticate a user agent with a server. Authorization header is used to provide authentication of the request for security purpose. Suppose that the server we are trying to access has enabled some basic authentication for accessing the data, in this case, we need to add Authorization Headers in PHP cURL Request with proper credentials like some Authentication key or password as specified by the API documentation you are using.

Every PHP cURL request include some basic steps which are given below:

  1. curl_init:- This step is used to initialize the cURL request session and it returns cURL session handler.
  2. curl_setopt:- This step is used to set cURL request options.
  3. curl_exec:- In this step, actual request is executed with the options set in the second step.
  4. curl_close:- After request gets executed, then we need to close cURL request session and free the all resources used by cURL.

cURL POST Data With Authorization Headers

We can use CURLOPT_HTTPHEADER option in the cURL to add Authorization Headers in the POST Request.

Following is the code used to send PHP cURL POST request with Authorization Headers:

<?php

//you can change your URL here
$url = 'https://www.codingambitions.com/test/api/login.php';

// post Data
$post_fields = array(
    'username' => "codingambitions",
    'password' => "12345"
);

//url-ify the data for the x-www-form-urlencoded POST data
foreach($post_fields as $key=>$value) {
    $post_fields_string .= $key.'='.$value.'&';
}
rtrim($post_fields_string, '&');

//init curl
$curl = curl_init();

//set curl options 
curl_setopt($curl,CURLOPT_URL, $url);
curl_setopt($curl,CURLOPT_POST, count($post_fields));
curl_setopt($curl,CURLOPT_RETURNTRANSFER ,true);
curl_setopt($curl,CURLOPT_POSTFIELDS, $post_fields_string );

//authKey can be any value as provided in API Panel
$authKey = "xyz12345";
$authHeaders = array();
$authHeaders[] = 'Content-Type: application/x-www-form-urlencoded';
$authHeaders[] = 'Authorization: '.$authKey;

//set authorization headers 
curl_setopt($curl, CURLOPT_HTTPHEADER,$authHeaders);

//execute the curl
$api_response = curl_exec($curl);
$err = curl_error($curl);

//close curl
curl_close($curl);

?>

Lets go through some important points in the above code. Here you may have noted the ‘Content-Type: application/x-www-form-urlencoded’, Content-Type defines the type of data we are sending in the request. For simple POST request, it is x-www-form-urlencoded data that we are sending. And last but important point is Authorization attribute in the headers, this is where we send the Authorization key to authenticate the request with the API server. And then we can see CURLOPT_HTTPHEADER option set in the curl, This is where we are telling cURL to include Authorization Headers in the Request.

So thats all about How To Post Data with Authorization Headers In PHP Using cURL. I hope you have understood the code.

Happy Coding!

Author

I am an core Android Developer with working knowledge of Kotlin and Java. And i also explore Flutter, React.js & Spring boot in extra time and having 8+ years of experience in this field. I have passion for solving complex problems. I love reading books and learning new challenging technologies in my extra time. Sharing my learning with others so that it can help others

Write A Comment