When a user makes a request to drupal through a browser, using the request URL drupal menu system decides which callback function to invoke.
what is a callback function?
Callback function in this case is a php function which gets called when a user trying to access a path that has already been registered in a menu item.
In the given example-1 if a browser makes a request using the URL "http://example.com/hello", menu system will look for a menu item which contains that path and invokes the relevant callback function in this case, "hello()".
example-1
<?php
//hook_perm implementation
function example_perm() {
return array(
'access hello'
);
}
//hook_menu implementation
function example_menu() {
$items = array();
//menu item
$items['hello'] = array(
'title' =--> t('hello'
), 'page callback' => 'hello', 'page arguments' => array(
0
), 'access arguments' => array(
'access hello'
), 'type' => MENU_CALLBACK);
return $items;
}
//callback function
function hello() {
echo "hello!";
}
?>
Output for example-1
hello!
example-2
This example shows how to get city data in json or xml using a request URL.
<?php
//hook_perm implementation
function example_perm() {
return array(
'access hello'
);
}
//hook_menu implementation
function example_menu() {
$items = array();
//menu item
$items['hello'] = array(
'title' => t('hello'
), 'page callback' => 'hello', 'page arguments' => array(
0
), 'access arguments' => array(
'access hello'
), 'type' => MENU_CALLBACK);
$items['full_city_list_json'] = array(
'title' => t('City List JSON'
), 'page callback' => 'full_city_list_json', 'page arguments' => array(
0
), 'access arguments' => array(
'access hello'
), 'type' => MENU_CALLBACK);
$items['full_city_list_xml'] = array(
'title' => t('City List XML'
), 'page callback' => 'full_city_list_xml', 'page arguments' => array(
0
), 'access arguments' => array(
'access hello'
), 'type' => MENU_CALLBACK);
return $items;
}
function hello() {
echo "hello!";
}
function full_city_list_json() {
$response = array();
$result = db_query("SELECT tid,name FROM term_data WHERE vid=2 order by name ASC");
while ($city_data = db_fetch_object($result)) {
$response[$city_data->name]['city_name'] = $city_data->name;
$response[$city_data->name]['tid'] = $city_data->tid;
}
if (!empty($response)) {
//encodes city data as json
$jsonstrCityList = json_encode($response);
echo $jsonstrCityList;
} else {
return false;
}
}
function full_city_list_xml() {
//sending city data as xml
$dom = new DOMDocument("1.0");
$root_elm = $dom->createElement("markers");
$root = $dom->appendChild($root_elm);
$result = db_query("SELECT tid,name FROM term_data WHERE vid=2 order by name ASC");
while ($city_data = db_fetch_object($result)) {
$marker_elm = $dom->createElement("marker");
$marker_node = $root->appendChild($marker_elm);
$marker_node->setAttribute("name", $city_data->name);
$marker_node->setAttribute("tid", $city_data->tid);
}
header("Content-type: text/xml");
echo $dom->saveXML();
}
?>
Outputs for example-2
for http://example.com/full_city_list_json
{"Agalawatta":{"city_name":"Agalawatta","tid":"156"},"Ahungalla":{"city_name":"Ahungalla","tid":"214"},"Akurana":{"city_name":"Akurana","tid":"164"},"Akuressa":{"city_name":"Akuressa","tid":"162"},"Aluthgama":{"city_name":"Aluthgama","tid":"120"},"Ambalangoda":{"city_name":"Ambalangoda","tid":"125"},"Ampara":{"city_name":"Ampara","tid":"211"},"Anuradhapura":{"city_name":"Anuradhapura","tid":"46"},"Arugambay":{"city_name":"Arugambay","tid":"114"},"Avissawella":{"city_name":"Avissawella","tid":"212"},"Badulla":{"city_name":"Badulla","tid":"108"},"Balangoda":{"city_name":"Balangoda","tid":"149"},"Balapitiya":{"city_name":"Balapitiya","tid":"486"},"Bandarawela":{"city_name":"Bandarawela","tid":"197"}}
for http://example.com/full_city_list_xml
<markers> <marker name="Agalawatta" tid="156"/> <marker name="Ahungalla" tid="214"/> <marker name="Akurana" tid="164"/> <marker name="Akuressa" tid="162"/> <marker name="Aluthgama" tid="120"/> <marker name="Ambalangoda" tid="125"/> <marker name="Ampara" tid="211"/> <marker name="Anuradhapura" tid="46"/> <marker name="Arugambay" tid="114"/> <marker name="Avissawella" tid="212"/> <marker name="Badulla" tid="108"/> <marker name="Balangoda" tid="149"/> <marker name="Balapitiya" tid="486"/> <marker name="Bandarawela" tid="197"/> </markers>
As in example-2 after you grabbed the city data in json or in xml you can parse them and use in any of your applications.






