PHP Set custom headers using cURL Example

Today, We want to share with you PHP Set custom headers using cURL Example.In this post we will show you How to set custom request header keys with curl and PHP?, hear for How to add the custom headers to the https request curl PHP script? we will give you demo and example for implement.In this post, we will learn about CURL API CALLS WITH PHP AND JSON DATA (GET POST PUT DELETE) with an example.

PHP Set custom headers using cURL Example

There are the Following The simple About Set custom headers using curl Example Full Information With Example and source code.

As I will cover this Post with live Working example to develop POSTing JSON Data With PHP cURL, so the PHP: HTTP Request Headers (cURL) for this example is following below.

Example 1: PHP SET/GET custom headers using cURL

$url = 'http://your_domain_nbame/api/v1/file.php';
$ch = curl_init($url);

$customHeaders = array(
    'Accept-Encoding: gzip, deflate, br',
    'Cookie: PHPSESSID=ciqas3vp98m692iubpr1b0md3o;',

curl_setopt($ch, CURLOPT_HTTPHEADER, $customHeaders);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);

echo $result;

PHP cURL Header

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Header-Key: Header-Value-1',
    'Header-Key-2: Header-Value-2'

How To POST JSON Data with PHP cURL

$data = array(
    'username' => 'admin_user',
    'password' => '989898989889'

$parameters = json_encode($data);

$ch = curl_init('');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($parameters))

$result = curl_exec($ch);


set custom header authorization in PHP curl?

$header = array(
    'Accept: application/json',
    'Content-Type: application/x-www-form-urlencoded',
    'Authorization: Basic '. base64_encode("your_app_key:your_app_secret")

curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

Web Programming Tutorials Example with Demo

Read :


You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about PHP custom headers using cURL.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Related FAQ

Here are some more FAQ related to this Article:

  1.   get product with Rakuten Marketing API using PHP
  2.   Laravel 6 Multiple Where Eloquent Query
  3.   Laravel Send Emails Mailgun setup Tutorial
  4.   Dynamically Change Alternate row colors using jQuery
  5.   Laravel 6 Insert Update and Delete record from MySQL
  6.   PHP Strong Random Number and String Generator
  7.   Angularjs Progress Bar Demo with Example
  8.   Bootstrap Toggle Inside JQuery DataTable
  9.   Remove specific value from php array
  10.   laravel checking if record exists