ItSolutionStuff.com

PHP Curl POST Request with Headers Example

By Hardik Savani • May 14, 2024
PHP

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

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

PHP Curl Request with Certificate (cert pem file option) Example

Read Now →

How to Upgrade PHP Version from 7.3 to 7.4 in Ubuntu?

Read Now →

How to Connect SSH using PHP?

Read Now →

PHP Get All Array Keys Starting with Certain String Example

Read Now →

PHP Change File Name in Folder Example

Read Now →

PHP Copy File from One Folder to Another Example

Read Now →

PHP Remove Directory and Files in Directory Example

Read Now →

Codeigniter Curl Post Request with Parameters Example

Read Now →

PHP CURL Post Request with Parameters Example

Read Now →

Laravel CURL Request Example using Ixudra/curl

Read Now →

PHP Download File from URL using CURL Request Example

Read Now →