4

I am developing a custom module and I need an URL that would trigger an action when called.

The purpose is for an external server to call this address passing some variables through $_REQUEST['message'], and then return a STRING with the success/error string. So the URL in question is only supposed to be called by a machine.

I now have a PHP file in the root that does the job, but I'd like to integrate it to the Drupal way through this custom module.... Is there a way to do this?

Thank you

8

The best way to do this is to build a custom module and implement the hook hook_menu().

You'll need to implement:

  • a .info file with metadata about your module
  • a .module file containing hook_menu() and your callback

foo.module should look like this:

function foo_menu() {
  $items = array();
  $items['foo/myurl'] = array( // change 'foo/myurl' to the url you want
    'title' => 'Callback',
    'type' => MENU_CALLBACK,
    'page callback' => 'custom_callback',
    'access arguments' => array('access content'),
  );
  return $items;
}

function custom_callback () {
  // Parse $_REQUEST parameters
  return 'success';
}
  • how can I make the custom_callback() function to echo some when visited from the browser? It should return a string (or an XML)... – Naoise Golden Jul 8 '11 at 10:06
  • Using echo() :) – Bart Jul 8 '11 at 10:14
  • maybe you can guide me on this one? drupal.stackexchange.com/q/6667/1146 (continuation of this one, but I'd rather make it a Question than a comment). Thanks – Naoise Golden Jul 8 '11 at 18:08

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.