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

Popular posts from this blog

Check validity of one time login url by Drupal 7 Services module?

Create mytut_service.info and put following code in file. I am assuming you know how to create module. name = Mytut service description = My custom Service. package = Other core = 7.x dependencies[] = services dependencies[] = rest_server Put following code in mytut_service.module file. 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...

Change picture mapping conditionally according to field value drupal 7

Hi Drupalars, Recently i got a task  "to provide an option in paragraph item to display a image grid 3 by 3 or 2 by 2" 3 by 3 grid 2 by 2 grid Adding conditional classes was fairly easy for each case , which is achieved by following code /** * Implements hook_preprocess_entity(). */ function YOURTHEME_preprocess_entity(&$variables) { if (!empty($variables['elements']['#entity']->field_show_50_50)) { if ($variables['elements']['#entity']->field_show_50_50['und'][0]['value'] == "1") { $variables['classes_array'][] = 'show-50-50'; }; } if (!empty($variables['elements']['#entity']->field_show_100)) { if ($variables['elements']['#entity']->field_show_100['und'][0]['value'] == "1") { $variables['classes_array'][] = 'show-100'; }; ...