Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Curl and PHP - how can I pass a json through curl by PUT,POST,GET

Tags:

post

put

php

curl

get

I have been working on building an Rest API for the hell of it and I have been testing it out as I go along by using curl from the command line which is very easy for CRUD

I can successfully make these call from the command line

curl -u username:pass -X GET http://api.mysite.com/pet/1 curl -d '{"dog":"tall"}' -u username:pass -X GET http://api.mysite.com/pet curl -d '{"dog":"short"}' -u username:pass -X POST http://api.mysite.com/pet curl -d '{"dog":"tall"}' -u username:pass -X PUT http://api.mysite.com/pet/1 

The above calls are easy to make from the command line and work fine with my api, but now I want to use PHP to create the curl. As you can see, I pass data as a json string. I have read around and I think I can probably do the POST and include the POST fields, but I have not been able to find out how to pass http body data with GET. Everything I see says you must attached it to the url, but it doesn't look that way on the command line form. Any way, I would love it if someone could write the correct way to do these four operations in PHP here on one page. I would like to see the simplest way to do it with curl and php. I think I need to pass everything through the http body because my php api catching everything with php://input

like image 278
Gilberg Avatar asked Jan 21 '14 23:01

Gilberg


People also ask

How pass JSON data in cURL post?

To post JSON data using Curl, you need to set the Content-Type of your request to application/json and pass the JSON data with the -d command line parameter. The JSON content type is set using the -H "Content-Type: application/json" command line parameter. JSON data is passed as a string.

How get post JSON in PHP?

To receive JSON string we can use the “php://input” along with the function file_get_contents() which helps us receive JSON data as a file and read it into a string. Later, we can use the json_decode() function to decode the JSON string.

How can get cURL output in JSON format in PHP?

To get JSON with Curl, you need to make an HTTP GET request and provide the Accept: application/json request header. The application/json request header is passed to the server with the curl -H command-line option and tells the server that the client is expecting JSON in response.


2 Answers

PUT

$data = array('username'=>'dog','password'=>'tall'); $data_json = json_encode($data);  $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data_json))); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response  = curl_exec($ch); curl_close($ch); 

POST

$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response  = curl_exec($ch); curl_close($ch); 

GET See @Dan H answer

DELETE

$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response  = curl_exec($ch); curl_close($ch); 
like image 103
voodoo417 Avatar answered Oct 20 '22 15:10

voodoo417


You can use this small library: https://github.com/ledfusion/php-rest-curl

Making a call is as simple as:

// GET $result = RestCurl::get($URL, array('id' => 12345678));  // POST $result = RestCurl::post($URL, array('name' => 'John'));  // PUT $result = RestCurl::put($URL, array('$set' => array('lastName' => "Smith")));  // DELETE $result = RestCurl::delete($URL);  

And for the $result variable:

  • $result['status'] is the HTTP response code
  • $result['data'] an array with the JSON response parsed
  • $result['header'] a string with the response headers

Hope it helps

like image 44
brickpop Avatar answered Oct 20 '22 16:10

brickpop