0

created a custom service resource to catch some data from the user through post method. but while I am accessing the service ie http://dev.localhost:8082/rest/send_mail/retrive.json

Query: curl -X POST -H "Accept-Encoding: gzip" -H "Content-Type: application/json; charset=utf-8"

sending the data in POST is : { "nid" :"1834", "recipient" :"charubachi.rath@abc.com", "sender":"charubachi.rath@abc.com", "subject": "xyz", "body":"a" }

How to accept the data

in my .module file

<?php

/**
 * Implementation of hook_services_resources().
*/
function send_mail_service_services_resources() {
 $api = array(
'send_mail' => array(
  'operations' => array(
    'create' => array(
      'help' => 'Drupal 7 Services 3 test',
      'file' => array('file' => 'inc', 'module' => 'send_mail_service'),
      'callback' => '_send_mail_service_create',
      'access callback' => 'user_access',
      'access arguments' => array('access content'),
      'access arguments append' => FALSE,
      'args' => array(
        array(
          'name' => 'nid',
          'optional' => FALSE,
          'source' => array('data' => 'nid'),
          'type' => 'int',
          'description' => 'The nid of the node to get',
        ),
        array(
          'name' => 'recipient',
          'type' => 'string',
          'description' => t('recipient'),
          'source' => array('data' => 'recipient'),
          'optional' => TRUE,
          'default' => '0',
        ),
        array(
          'name' => 'sender',
          'type' => 'string',
          'description' => 'sender',
          'source' => array('data' => 'sender'),
          'optional' => TRUE,
          'default' => '0',
        ),
        array(
          'name' => 'subject',
          'type' => 'string',
          'description' => 'subject',
          'source' => array('data' => 'subject'),
          'optional' => TRUE,
          'default' => '0',
        ),
        array(
          'name' => 'body',
          'type' => 'string',
          'description' => 'body',
          'source' => array('data' => 'body'),
          'optional' => TRUE,
          'default' => '0',
        ),
      ),
    ),
  ),
),
);
 return $api;
}

in my call back function when I am trying to print $_POST I am getting empty values.

  • Try printing $data in your services callback. – niksmac Mar 13 '14 at 15:34
0

You are passing every element as individual element, so you can't get these values in $_POST array. I have executed your code at my end and retrieve each element value by following call back function.


function _send_mail_service_create($nid, $recipient, $sender, $subject, $body) {
  echo "nid => ".$nid;
  echo "\nrecipient => ".$recipient;
  echo "\nsender => ".$sender;
  echo "\nsubject => ".$subject;
  echo "\nbody => ".$body;  
}

Output: nid => 1834 recipient => charubachi.rath@abc.com sender => charubachi.rath@abc.com subject => xyz body => a

If you want to have these values in an array then you can use this in following way:


 /**
   * Implementation of hook_services_resources().
   */
  function send_mail_service_services_resources() {
    $api = array(
      'send_mail' => array(
        'operations' => array(
          'create' => array(
            'help' => 'Drupal 7 Services 3 test',
            'callback' => '_send_mail_service_create',
            'access callback' => 'user_access',
            'access arguments' => array('access content'),
            'access arguments append' => FALSE,
            'args' => array(
              array(
                'name' => 'title',
                'optional' => FALSE,
                'source' => 'data',
                'type' => 'string',
                'description' => 'The data collected through JSON',
              ),
            ),
          ),
        ),
      ),
    );
 return $api;
}


function _send_mail_service_create($arg) {
  print_r($arg);
}


Output: Array ( [nid] => 1834 [recipient] => charubachi.rath@abc.com [sender] => charubachi.rath@abc.com [subject] => xyz [body] => a )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.