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;',
    'Referer: https://www.google.com/'
);

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

<?php
$data = array(
    'username' => 'admin_user',
    'password' => '989898989889'
);

$parameters = json_encode($data);

$ch = curl_init('https://api.your_domain_name.com/api/1.0/user/signin');
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);
curl_close($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 :

  PayPal Payment Gateway with PHP MySQL Database

Summary

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 infinityknow.com 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.

Leave a Reply

avatar
  Subscribe  
Notify of