ItSolutionStuff.com

PHP CURL Post Request with Parameters Example

By Hardik Savani • May 14, 2024
PHP CURL

Sometime we need to work with web services and APIs of third party website, at that time we need to use php curl for get request, post request, delete request, put request ect. php curl will help to post request with parameters and headers, we can get json response.

PHP cURL have set of curl function like curl_init(), curl_setopt(), curl_exec() etc. using cURL we will call apis to getting json data and we can use their data in our project.

Here, i will give you very simple example of curl request and also give you 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 */

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));

/* 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 */

curl_setopt($ch, CURLOPT_HTTPHEADER, array(

'Content-Type:application/json',

'App-Key: 123456',

'App-Secret: 1233'

));

/* 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

How to Get Difference Between Two Dates in PHP?

Read Now →

PHP MySQL Column Sorting Example Tutorial

Read Now →

PHP JQuery Chosen Ajax Autocomplete Example

Read Now →

PHP Ajax Multiple Image Upload with Preview Example

Read Now →

PHP MySQL Confirmation Before Delete Record using Ajax Example

Read Now →

PHP MySQL DataTables Server-side Processing Example

Read Now →

Laravel CURL Request Example using Ixudra/curl

Read Now →

PHP JQuery Ajax Image Upload Example Tutorial

Read Now →

PHP Ajax Dependent Dropdown List Example

Read Now →

Tags Input with Autocomplete using jQuery and PHP Example

Read Now →

PHP MySQL Highcharts Chart Example

Read Now →

Multiple File Upload using Dropzone JS in PHP Example

Read Now →

PHP Crop Image Before Upload using Croppie JS Example

Read Now →

PHP Download File from URL using CURL Request Example

Read Now →