How to create resource,end point and use web service using Services module in Drupal?

As per my requirement, I was to fetch some data from third party and store into Drupal database.

For this, I created my own web service using Services module.
Here are steps for creating Service -


Note - I developed web service using Services 6.x module which is not in projects now in drupal.org.

So here steps are based on the Drupal6. 

1. First of all install services and all its dependent modules.

2. Go to the admin/build/services and click on Add to add end point.

3. Give respective name to the end point and path to endpoint for your service.(I would suggest to you keep same name for both).

4. In server drop down, you will found XML/RPC and REST by default.(Only if you have enabled these two modules from admin).

5. I will suggest you to use soap_server module.(Its easy to implement and test). Install soap_server and enable it. then select it form server drop down while adding end point. Leave other settings as it is.

6. Once you save your end point, you can see Resources, Server, Authentication etc links. Our main focus in Resources. There will be some default resources listed  but here we have to add our resource.

7. For adding resource, create a module. and add hook_services_resources - 


/**

 * Implementation of hook_services_reources().

 */

function hook_services_resources() {

  return array(

    // change your_resource_name

    'your_resource_name' => array(

    // change your_opration_name_under_resource

     'your_opration_name_under_resource' => array(

        'help' => 'Add help text here',

        'callback' => '_survey_custom_survey_data',     // change callback function

        'access callback' => '_survey_custom_survey_data_access',  // change access callback

        // Keep below settings as it is

        'access arguments append' => TRUE,

        'args' => array(

          array(

            'name' => 'input_xml',

            'type' => 'string',

            'description' => 'Data in XML Format',

            'source' => array('path' => '0'),

            'optional' => FALSE,

          ),

        ),

      ),

    ),

  );

}


/**

 * Implementation of Resource access callback().

 */

function _survey_custom_survey_data_access() {

  return TRUE;

}


/**

 * This is th main callback function which will recieve data when service gets called().

 * It will recieve data in json format and decode it to store in Drupal database.

 */

function _survey_custom_survey_data($json_object) {

  $decoded_json_data = json_decode($json_object);

  return $decoded_json_data;

 }

 

8. Next step is to select this resource for the particular end point. Click on Edit Resources in admin/build/services for the end point you want to add this resource. You can see your create resource will be appear in the resource list. Select it.


9. Its time to test the service. For this create one test.php file.This file you can create in some other server add below code -

<?PHP

    ini_set('display_errors', 'On');

 // Add full url of your domain, where you have create "your_resource_name" end point.

// We are concatenating $base_url with resource name which we have create using code to create wsdl.

    $client = new SoapClient("http://your-domain/your_resource_name?wsdl");

// Third party who will use this service, they need to create json object of data and pass like below

    $json_object ='[{"FieldName":"survey_id","FieldValue":"110"},{"FieldName":"survey_title","FieldValue":"Century Survey"}]';

 // Here you need to call method name and pass json object.

 // To get method name, you can copy and paste wsdl url in web browser and check the opration name. That will be your method name.

    $result = $client->module_name_soap_your_resource_name($json_object);

    print '<pre>';

    print_r($result);

    die;

?>


10. This function _survey_custom_survey_data() will return decoded json of the data which we have recieved from service.


1 comment: