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.

Read Also:  Automatic Image slideshow Using PHP

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 :

Read Also:  Angular 6 applications - Insert, Fetch , Edit - update , Delete Operations


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.

Review Date
Reviewed Item
Author Rating

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  PHP Tutorial
  2. Read Also:  Wordpress Plugin uninstall hook - register_uninstall_hook()
  3. Read Also:  PHP cURL Requests with Parameters Example
  4. Read Also:  PayPal Payment Gateway with PHP MySQL Database
  5. Read Also:  how to get checkbox value in php if checked?
  6. Read Also:  How to add Ajax loader image in PHP?
  7. Read Also:  Laravel 5/6/7 Carbon date format Example
  8. Read Also:  Laravel Group By Count Multiple Columns
  9. Read Also:  PHP CURL POST and GET REQUEST Methods
  10. Read Also:  Menu Based Program in PHP Using Switch case