PHP Curl POST Request with Headers Example
Hi,
This post will give you example of php curl post request with headers example. In this article, we will implement a how to send headers in curl php. i explained simply step by step how to send headers in php curl. we will help you to give example of php curl post request header. Here, Creating a basic example of php curl post data header.
PHP cURL have set of curl function like curl_init(), curl_setopt(), curl_exec() etc. using cURL we will call post api with headers to getting json data and we can use their data in our project.
Here, i will give you very simple example of curl post request with headers with authentication example on bellow of simple curl request example:
Simple Example:
<?php
/* API URL */
$url = 'http://www.mysite.com/api';
/* Init cURL resource */
$ch = curl_init($url);
/* Array Parameter Data */
$data = ['name'=>'Hardik', 'email'=>'itsolutionstuff@gmail.com'];
/* pass encoded JSON string to the POST fields */
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
/* set the content type json */
$headers = [];
$headers[] = 'Content-Type:application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
/* set return type json */
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
/* execute request */
$result = curl_exec($ch);
/* close cURL resource */
curl_close($ch);
>
Header Auth Example:
<?php
/* API URL */
$url = 'http://www.mysite.com/api';
/* Init cURL resource */
$ch = curl_init($url);
/* Array Parameter Data */
$data = ['name'=>'Hardik', 'email'=>'itsolutionstuff@gmail.com'];
/* pass encoded JSON string to the POST fields */
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
/* set the content type json */
$headers = [
'Content-Type:application/json',
'App-Key: 123456',
'App-Secret: 1233'];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
/* set return type json */
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
/* execute request */
$result = curl_exec($ch);
/* close cURL resource */
curl_close($ch);
>
Now you can check it
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- PHP Curl Request with Certificate (cert pem file option) Example
- How to Upgrade PHP Version from 7.3 to 7.4 in Ubuntu?
- How to Connect SSH using PHP?
- PHP Get All Array Keys Starting with Certain String Example
- PHP Change File Name in Folder Example
- PHP Copy File from One Folder to Another Example
- PHP Remove Directory and Files in Directory Example
- Codeigniter Curl Post Request with Parameters Example
- PHP CURL Post Request with Parameters Example
- Laravel CURL Request Example using Ixudra/curl
- PHP Download File from URL using CURL Request Example