Skip to main content

Creating custom Web Service along with passing parameters Drupal 7

Put the following code in .module file. And enable this module, i am assuming you know how to create module.

 function mytut_service_services_resources() {  
   $api = array(  
     'mytut_service_service' => array(  
       'operations' => array(  
         'retrieve' => array(  
           'help' => 'Retrieves story by nid',  
           'callback' => 'mycallback',  
           'access callback' => 'user_access',  
           'access arguments' => array('access content'),  
           'access arguments append' => FALSE,  
           'args' => array(  
             array(  
               'name' => 'fn',  
               'type' => 'string',  
               'description' => 'Function to perform',  
               'source' => array('path' => '0'),  
               'optional' => TRUE,  
               'default' => '0',  
             ),  
             array(  
               'name' => 'ts',  
               'type' => 'int',  
               'description' => 'timestamp',  
               'source' => array('param' => 'ts'),  
               'optional' => TRUE,  
               'default' => '0',  
             ),  
             array(  
               'name' => 'hpwd',  
               'type' => 'string',  
               'description' => 'hashed password',  
               'source' => array('param' => 'hpwd'),  
               'optional' => TRUE,  
               'default' => '0',  
             ),  
             array(  
               'name' => 'uid',  
               'type' => 'int',  
               'description' => 'user id',  
               'source' => array('param' => 'uid'),  
               'optional' => TRUE,  
               'default' => '0',  
             ),  
           ),  
         ),  
       ),  
     ),  
   );  
   return $api;  
 }  
 /**  
  * Callback function for blog retrieve  
  */  
 function mycallback($fn, $ts, $hpwd, $uid) {  
   return array($fn, $ts, $hpwd, $uid);  
 }  





Create Services endpoint as shown in following image.

You will notice additional resource in endpoint listing as shown in image. Select this resource and save.


My endpoint url is "data". So proper url to use this endpoint will be
YOUR_DOMAIN_NAME/?q=data/mytut_service_service/retrieve?&ts=1432539547&hpwd=IyHk9GeJS03Eu7ZQwr9G2Ob8JUCn7Lbeu6Or9S_x4wY&uid=230

And response you will get is shown in image.I passed three parameters here

Comments