$_POST returns empty value in custom service

Solutions

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 => [email protected] sender => [email protected] 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] => [email protected] [sender] => [email protected] [subject] => xyz [body] => a )

Tags: Services

Similar questions

How can I get $_POST data in a callback?
I've implemented hook_menu roughly as follows: I want to be able to post data to this url. So says $_POST is the following array: How can I access $_POST['data1'] and $_POST['data2'] inside the callback? Directly accessing $_POST seems to not work (it's var_dumped as empty).
Grabbing $_POST data
A quick summary of where I stand right now: We have a low traffic site here that was redone in Drupal about 8 months ago, before I was in the picture. An external design firm sold the owners on the technology as a way to enable non-programmers to update site content easily. Unfortunately, they did it wrong - they built our theme in-place over the B...
drupal store and retrieve variable from $_POST array
This is really simple question for the user who are using DRUPAL for many years.But for me it is creating a big issue. I already made a form using the drupal form api.But after submit the form I want to store form data in the "$_POST" and print the data to another module's page.I get the data on the 2nd page(module's page where I want to show the f...
How can I add a sub-array structure in my results? $_POST
I'm building a form. In that form I have a lot of checkboxes. Normally, It's possible to make an array of some checkboxes (with name attribute). Normally I would have this result with print_r($_POST); I tried to add the name attribute, but the $_POST takes the structure of $form. But how can I add a sub-array structure in my results? This is what I...
How to get $_POST and $_GET parameters
I ajaxly call a route and pass data to it, In my controller, I want to get $_POST['var'] , $_POST['var2'], it seems because of using HttpFoundation in drupal 8, $_POST and $_GET are not exists in old method, How I can access $_POST parameters in custom Controller?
Use $_POST after submit form
I create a form and a page with controller in Drupal 8. I want print value of my example input after submit form. For example my input name is "Tel_number". My code in controller page is: But no resault print.

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.