How to render data from external API With my current module I have a simple form where a user inputs some information, that information is then sent to an external API, and that API sends some data back.

How do I display the data that is returned from the external API?

Thanks

&lt;?php
/**
 * Implements hook_menu();
 */
function find_rep_menu(){
    $items = array();
    $items['find-representatives/%'] = array(
        'title' =&gt; 'My Form',
        'page callback' =&gt; 'find_representative_value_callback',
        'page arguments' =&gt; array(2),
        'access callback' =&gt; TRUE,
    );
    return $items;
}

/**
 * My form
 */
function find_rep_form($form, &amp;$form_state) {
    $form['address'] = array(
        '#type' =&gt; 'textfield',
        '#title' =&gt; t('Your Address'),
        '#size' =&gt; 60,
        '#maxlength' =&gt; 128,
        '#required' =&gt; TRUE,
    );
    $form['submit'] = array(
        '#type' =&gt; 'submit',
        '#value' =&gt; t('Find Rep'),
    );
    return $form;
}

/**
 * Submit handler for the find_rep_form.
 */
function find_rep_form_submit($form, &amp;$form_state) {
    $address = $form_state['values']['address'];
    fetchGeocode($address);
    $fetchGeocode = fetchGeocode($address);
    fetchStateInfo($fetchGeocode['returnedLat'], $fetchGeocode['returnedLong']);
    $form_state['redirect'] = 'find-representatives/' . $form_state['values']['address'];
}

function find_representative_value_callback($address) {
    return t('Value: @value', array('@value' =&gt; $address));
    //Once I was able to get this working, I was going to push my returned data through here
}
    /*
     * The fetch code
     */
    function fetchGeocode($userAddress) {
        $userAddress = urlencode($userAddress);
        $fetchUrl = "https://api.mapbox.com/geocoding/v5/mapbox.places/" . $userAddress . ".json?country=us&amp;proximity=38.8977%2C%2077.0365&amp;types=address&amp;autocomplete=true&amp;access_token=APIKEY";
        $result = drupal_http_request($fetchUrl);
        if (in_array($result-&gt;code, array(200, 304))) {
            $geoResponse = json_decode($result-&gt;data);
            $long = $geoResponse-&gt;features[0]-&gt;geometry-&gt;coordinates[0];
            $lat = $geoResponse-&gt;features[0]-&gt;geometry-&gt;coordinates[1];
            return array('returnedLat' =&gt; $lat, 'returnedLong' =&gt; $long);
        } else {
            drupal_set_message("Geocode returned: " . $result-&gt;code); //For testing purposes
        }
    }

    function fetchStateInfo($userLat, $userLong) {
        $userLat = urlencode($userLat);
        $userLong = urlencode($userLong);
        $fetchUrl = 'http://openstates.org/api/v1//legislators/geo/?lat=' . $userLat . '&amp;long=' . $userLong . '&amp;apikey=APIKEY';
        $result = drupal_http_request($fetchUrl);
        if (in_array($result-&gt;code, array(200, 304))) {
            $stateInfo = json_decode($result-&gt;data, true);
            drupal_set_message($stateInfo[0]['full_name']);
        } else {
            drupal_set_message("State API Returned Error: " . $result-&gt;code);
        }
    }


I was taking some examples from example two on this comment here
If you don't need to save the data returned by the external source in your drupal database, then you can go through below mentioned points,


In hook_form_form_id_alter declare a custom submit function

$form['#submit'][] = "my_module_form_submit_handler";
in the custom submit function redirect after submission.(are you using cURL to send and receive data to and from external api.)


$value1 = 'value returned from external api';
$value2 = 'value returned from external api';
$form_state['redirect'] = 'myurl/' . $value1.'/'.$value2 ;


Create a menu item with hook_menu and pass the values as argument

$menu['myurl/%/%'] = array
(
  'title' => 'some title',
  'page callback' => 'my_page_callback',
  'page arguments' => array(1, 2), // 1 and 2 are the first and second indexes of the URL - the location of the wildcards
  'access callback' => TRUE,
);
in page callback function pass the variables

function my_page_callback($val1,$val2)
{
return theme('custom_data_page', array('val1' => $val1,'val2' => $val2));

}
implement hook_theme 


function mymodule_theme($existing, $type, $theme, $path) {
    return array(
        'custom_data_page' => array(
            'variables' => array(
                'val1' => val1,
                'val2' => val2
            ),
            'template' => 'your_template_filename', // do not include .tpl.php
            'path' => 'path-to-your-template-file'
        )
    );
}


Create the template file in templates folder
Clear cache.
Let me know if you face any issues