REST API provides external interface to call some important functions of any Web based software. It’s very easy to write and test applications with REST APIs. Let’s say that we are building an application that we want to automatically upload an image file and submit a form data. We can perform this work through some APIs if provided. We suppose sample REST APIs and then write the code to call these REST APIs. We will use on the POST method for sending data to the server.


The first for any REST API is the URLs. We assume our base URL to be: 

Authentication information is also required for calling secure web services. We suppose following:

username: username
password: secret

We also need the information about REST API functions that we need to call. We assume following two REST functions.

Method: Image_Upload

Input Parameters:
* title - Name of the image 
* media: Path to the image file preceeded by a @ sign

Returns json encoded string containing true and image_id if successful. 
Method: Contact_Create

Input Parameters:
* first_name
* last_name
* email

Returns json encoded string containing contact_id if successful.

Authentication and Preparing Request

Every REST API call requires authentication before calling the web service method. Following function rest_api_call is used by clients to authenticate requests to the REST API. It will also prepare the request and carry the request to REST Web Service. It has two arguments. First argument is the name of the API method, where as second argument takes an array containing the input data to the method.

    function rest_api_call($method, $arguments = array()) {
      $api_url  = '';
      $username = 'myuser';
      $password = 'mysecret';
      $service_url = "$api_url/$method?username=$username&password=$password";
      $curl = curl_init($service_url);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($curl, CURLOPT_POST, true);
      foreach($arguments as $key=> $value) {
          $arguments[$key] = json_encode($value);
      curl_setopt($curl, CURLOPT_POSTFIELDS, $arguments);
      $curl_response = curl_exec($curl);
      return json_decode($curl_response);  

Prepare Arguments and Call rest_api_call()

In this section, concrete example is given to demonstrate how to prepare arguments and then call rest_api_call function.

This example uploads image file. Here first argument is created in an array. Argument array contains name and value pairs. Names like ‘title’ and ‘media’ are the input parameters of the REST function Image_Upload. While ‘myimage’ and ‘@testpath/img.jpg’ are the values that will passed to the Image_Upload function. Then rest_api_call() is called by passing the arguments array and the REST method name i.e. Image_Upload.

    $arguments = array(
     'title' => 'myimage',
     'media' => '@testpath/img.jpg',
    $result = rest_api_call('Image_Upload', $arguments);
    $result = $result[1];
    $img_id = $result->img_id;

Similarly another REST API call code is given below.

$arguments = array('first_name'=>'chulbul', 'last_name'=>'pandey','email'=>'');
$result  = rest_api_call('Contact_Create', $arguments);
$contact = $result[1];

As you see above, calling a REST API code reduces to just three lines of code. The authentication and preparing request part is written just once in rest_api_call method.

This is post demonstrates the client-side code of REST APIs. The Server-side code for the Upload_Image and Contact_Create functions are not shown. It is assumed while calling the Web services that the client is only familiar with Web service name, its input arguments and output values. That is why we have only focused on these things and to make it look that calling REST web services is very simple and easy.

Tagged with: PHPSource Code

One Response to Calling REST API From PHP – The Easy Way

  1. This article is really a fastidious one it assists new web users, who are
    wishing for blogging.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds

Set your Twitter account name in your settings to use the TwitterBar Section.