PHP : How to use 4 methods (DELETE, GET, POST, PUT) in a RESTful API client using Curl ?

Finishing my first release of PhpRestDoc I had to develop a cross-method solution to allow users to test REST API requesting directly from the documentation and get 3 parts of the API response :

  • HTTP Status Code,
  • Header,
  • Body,

through 4 different methods :

  • DELETE,
  • GET,
  • POST,
  • PUT.

Also I wanted to be able to set what content type I wanted to get (JSON, XML, etc).

I then fully commented to share you the source code I wrote to achieve that :

/*
  Set the Request Url (without Parameters) here
*/
$api_request_url = 'https://api.example.com/user/info/';

/*
  Which Request Method do I want to use ?
  DELETE, GET, POST or PUT
*/
$method_name = 'GET';

/*
  Let's set all Request Parameters (api_key, token, user_id, etc)
*/
$api_request_parameters = array(
  'api_key' => 'gF7mc05157W726sL',
  'token' => 'hnFGJSpNzMzft8GE8AVTdvyZaydNYorgthkO46Rt',
  'user_id' => 5428
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

if ($method_name == 'DELETE')
{
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
  curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($api_request_parameters));
}

if ($method_name == 'GET')
{
  $api_request_url .= '?' . http_build_query($api_request_parameters);
}

if ($method_name == 'POST')
{
  curl_setopt($ch, CURLOPT_POST, TRUE);
  curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($api_request_parameters));
}

if ($method_name == 'PUT')
{
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($api_request_parameters));
}

/*
  Here you can set the Response Content Type you prefer to get :
  application/json, application/xml, text/html, text/plain, etc
*/
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));

/*
  Let's give the Request Url to Curl
*/
curl_setopt($ch, CURLOPT_URL, $api_request_url);

/*
  Yes we want to get the Response Header
  (it will be mixed with the response body but we'll separate that after)
*/
curl_setopt($ch, CURLOPT_HEADER, TRUE);

/*
  Allows Curl to connect to an API server through HTTPS
*/
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

/*
  Let's get the Response !
*/
$api_response = curl_exec($ch);

/*
  We need to get Curl infos for the header_size and the http_code
*/
$api_response_info = curl_getinfo($ch);

/*
  Don't forget to close Curl
*/
curl_close($ch);

/*
  Here we separate the Response Header from the Response Body
*/
$api_response_header = trim(substr($api_response, 0, $api_response_info['header_size']));
$api_response_body = substr($api_response, $api_response_info['header_size']);

// Response HTTP Status Code
echo $api_response_info['http_code'];

// Response Header
echo $api_response_header;

// Response Body
echo $api_response_body;

9 Comments

  1. Anon   •  

    This little gem here:

    $api_response_body = substr($api_response, $api_response_info[‘header_size’]);

    has saved me.

    Thanks so much.

  2. bheem   •  

    Hi
    Can you please tell me how to get POST response in REST (PHP) ?
    How can I check what parameters are sending from apps?
    I am new in web services.Please assist me

  3. bheem   •  

    I am using json format,but getting blank array

  4. Yacine   •  

    Thanks a lot for this piece of code. it helped me a lot.

  5. Wale   •  

    You are a life saver! Thanks.

  6. sumit   •  

    can you plz put the compelte source code in github….

  7. Mark M Owuya   •  

    How do I call for the request method in the parameters?
    Like PUT, POST, DELETE?
    “http://url.com/api/params” <- where in there do I specify them, so that the request knows??

    I've been looking around for like 2 days without any good answer.
    This would be greatly appreciated, thanks.

  8. Andre   •  

    Hi, thank you for the PUT example. The problem I am having is how to read the PUT data. It does not come through as POST or any other form. I can’t find a way to read it. Do you know how to read the PUT data using PHP?

Leave a Reply

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