Last updated September 26, 2012. Created on September 5, 2011.
Edited by jackbravo, lelizondo. Log in to edit this page.

Here is a small module to access a REST server using PHP. The module assumes that you already have services installed with a REST server working and that you've already created some nodes.

This module gets a node and prints the node and body. It's really simple and is just meant as a demo.

File: restget.module

<?php
/**
 * Implements hook_menu()
 */
function restget_menu() {
   
$items['test'] = array(
       
'title' => 'Test',
       
'page callback' => 'restget_page',
       
'access callback' => TRUE,
       
'type' => MENU_NORMAL_ITEM
   
);
   
    return
$items;
}

/**
 * Get the element as object
 * @param $uri the URI to get
 * @return object
 */
function _get_element() {
       
// Get the node with nid 31 using JSON format.
   
$uri = "http://localhost/mysite/api/rest/node/31.json";
   
$response = file_get_contents($uri);

       

// This will return an array, if you want an object, use json_decode($response) directly. See the comments below
   
return drupal_json_decode($response);
}

/**
 * Page callback
 */
function restget_page() {
   
$element = _get_element();
   
   
// See the complete object with
    // return dprint_r($element);
   
        // If you used drupal_json_decode in _get_element() you'll have an array instead of an object
   
$output = '<h2>' . $element->title . '</h2>';
   
$output .= '<p>' . $element->body->und[0]->value . '</p>';
   
    return
$output;
}
?>

Be aware that you're not supposed to return HTML inside a function like I do in restget_page, to do that, you're supposed to create a theme function and pass the $output to that theme function, but since this is just a demo module I didn't want to over complicate

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

davmorr’s picture

When converting the REST json string, drupal_json_decode() returns the node object as an array, not an object as the example suggests in the restget_page() page callback function in the above example. The following should illustrate this better:

<?php
function restget_page() {
   
$element = _get_element();
   
   
/*
    // See the complete object array with dsm() or dprint_r) if the Devel module is not enabled
    if (module_exists('devel')) dsm($element);
    else return dprint_r($element);
    */
   
   
$lang = $element['language'];
   
$output = '<h2>' . $element['title'] . '</h2>';
   
$output .= '<p>' . $element['body'][$lang][0]['value'] . '</p>';
   
    return
$output;
}
?>
lelizondo’s picture

You're right but I feel like I need to explain why this happened. The code originally used json_decode instead of drupal_json_decode, by default, json_decode returns an object, not an array. drupal_json_decode is just a wrapper function that calls json_decode

function drupal_json_decode($var) {
  // The TRUE argument means the output will be an array, if FALSE it will return an object. Default is FALSE.
  return json_decode($var, TRUE);
}

If you take a look at the revisions, originally, I was using json_decode directly, then I changed it but I didn't realize until now that using drupal_json_decode returns an array.

Conclusion: if you want an object instead of an array, don't use drupal_json_decode, use json_decode directly.

Luis

quocnam15’s picture

If node content has image how to i can retrieve and display it?

lelizondo’s picture

you have to take the image field and with the path, you can use theme_image

Luis