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
For myself, I just encode it in the url and use $_GET on the destination page. Here's a line as an example.
EDIT: Adding the destination snippet... (EDIT 2 added more above at OPs request)
PUT
POST
GET See @Dan H answer
DELETE
set one more property curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , false);
You can use this small library: https://github.com/jmoraleda/php-rest-curl
Making a call is as simple as:
And for the $result variable:
Hope it helps
I was Working with Elastic SQL plugin. Query is done with GET method using cURL as below:
I exposed a custom port at public server, doing a reverse proxy with Basic Auth set.
This code, works fine plus Basic Auth Header: