PHP curl post request with parameters and get json response

PHP curl post request with parameters and get json response

In this post we will give you information about PHP curl post request with parameters and get json response. Hear we will give you detail about PHP curl post request with parameters and get json responseAnd how to use it also give you demo for it if it is necessary.

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:

/* API URL */

$url = 'http://www.mysite.com/api';

/* Init cURL resource */

$ch = curl_init($url);

/* Array Parameter Data */

$data = ['name'=>'Hardik', 'email'=>'onlinecode@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:

Also see:PHP MySQL confirmation box before delete record using jquery ajax

/* API URL */

$url = 'http://www.mysite.com/api';

/* Init cURL resource */

$ch = curl_init($url);

/* Array Parameter Data */

$data = ['name'=>'Hardik', 'email'=>'onlinecode@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…

Hope this code and post will helped you for implement PHP curl post request with parameters and get json response. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  81  =  91

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US