<?php
//=============================================================================
// Call a Web Service
//=============================================================================

error_reporting(E_ALL ^ E_WARNING ^ E_NOTICE);  //all except warnings & notices

    $url       = 'workshop.sps.nyu.edu/~sultans/util/rest/REST.php';
    $url_param = $url .'?'. 'user=demo&pswd=demo&db=demo&format=json';

    $sql = $_GET['sql'];                                #get SQL param field
    
//  $sql = 'select * from student';                     #for testing only     

    if (! $sql)
    {
        print "<h3>Enter URL?sql=... </h3> \n";
        exit;
    }

    print('<h3>PHP pgm Calling a PHP Web Service</h3>');

    $sql =  urlencode($sql);                            #encode the param 

    $url_param .= '&sql=' . $sql;                       #append the SQL to end of param

//	print($url_param);									#for debugging

//  $str = file_get_contents($url_param);               #retrieve the url - No longer workds for https

	$c = curl_init($url_param);
	curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
	$str = curl_exec($c);
	curl_close($c);

//	print ("<br> result $str");							#for debugging

    $array = json_decode($str, true);                   #convert JSON string into nested PHP array
//  var_dump($array);                                   #print the array structure
    
    for ($i=0; $i < count($array); $i++)                #loop through outer array
    {
        $row = $array[$i];
        foreach ($row as $colName => $colValue)         #loop through inner assoc array
            print "$colName => $colValue <br>";
        print ('<br>');
    }
 
?>