You are here

Cross Domain Access to JSON Data Returned by Drupal's Hook Menu

In this article I will talk about how I access JSON data processed by a Drupal Hook Menu's page callback from a remote domain. The idea here is that sometimes we want to take advantage of Drupal powerful Hook Menu (hook_menu) system to register paths and handle the processing of data for that page request.

HOOK MENU

hook_menu

First, I'll add a menu item so that we can register the path that when access will return an object in JSON format. for this example, I will use the name mymodule to refer to the module where all the code will be saved.

/**
* Implementation of hook_menu().
*/
function mymodule_menu() {
  $items = array();
  $items['stock-market/ticker-symbols/json'] = array(
    'title' => 'Stock Market Ticker Symbols',
    'page callback' => '_get_ticker_symbols',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

Things worth mentioning about this sample menu item:

  1. stock-market/ticker-symbols/json is the path that we are registering.
  2. page callback is the function that will handle the request when this page is accessed.
  3. access arguments setting this permission to array('access content') means that I allowed Anonymous Users to access content. If you don't want to give such general permission, you can either set this value to TRUE or use the Hook Access (hook_access) to define access restrictions to this specific menu item. Visit Drupal API to learn more about hook_access

So now that we have register the path, we have to define the page callback function which will process and return the data in JSON format.

function _get_ticker_symbols() {
  $tickers = array(
    'AAPL' => 'Apple Inc.',
    'MSFT' => 'Microsoft Corp.',
    'ORCL' => 'Oracle Corporation'
  );

  print $_GET['jsoncallback'] . '(';
  print drupal_json(
    array (
      'tickers' => $tickers,
    )
  );
  print ')';
  die; // Do not processed hook_theme since we only want to return a JSON object
}

Since we need cross-domain/remote access to the data, we need to wrap the JSON data in a function call (JSONP). In this case jsoncallbackis our function call so that data is return with this structure:

? ({"AAPL": "Apple Inc.", "MSFT": "Microsoft Corp.", "ORCL" => "Oracle Corporation"})

For this example, I am going to use JavaScript to access this data remotely. This JavaScript file will access this data by performing an Ajax call to the remote server via JQuery.getJSON().

$(document).ready(function () {
  _url = "http://www.roldanarts.com/stock-market/ticker-symbols/json";
  $.getJSON(_url + "?jsoncallback=?", function(data) {
    alert(data.tickers);
  });
});

Now when you use this JavaScript from a remote server you will be able to access the data and use it to be displayed or store.

References:

  • Hook Menu - hook_menu()
  • Drupal JSON - drupal_json()
  • JQuery getJSON - jQuery.getJSON()
  • Hook Access - hook_access()

lifestyle: