<html>
<head>
<title>Use REST Web Service</title>
<style type="text/css">
#Layer1 {
        position:relative;
        left:30px;
        top:10px;
        width:700px;
        height:230px;
}
</style>
</head>

<body bgcolor="lightyellow">
<h1>Call a PHP REST Web Service using PHP</h1>

<?php
    error_reporting
(E_ALL E_WARNING E_NOTICE);  //all except warnings & notices
    

//  $url = 'http://workshop.sps.nyu.edu/~sultans/php/demo/ws/REST.php';     //Does not work due to .htaccess
    
$url 'http://workshop.sps.nyu.edu/~sultans/util/rest/REST.php';       //the REST Web Service

    
if($_POST)
        
retrieve();

    
display();

//------------------------------------------------------------------------------------------------------
function retrieve()
{
        global 
$url$user$pswd$db$sql$format$show$msg$data;

        
$user    $_POST['user'];              //the database user
        
$pswd    $_POST['pswd'];              //the database password
        
$db      $_POST['db'];                //the database name
        
$sql     $_POST['sql'];               //the SQL statement
        
$format  $_POST['format'];            //the desired output format
        
$show    $_POST['show'];              //if the client wants to see the returned data as is

        
if (!$user || !$pswd || !$db || !$sql || !$format) {
                
$msg 'Please enter user, pswd, database and SQL';
                return;
        }

       
$sql2 urlencode($sql);

        
$url .= '?user='   $user
             
.  '&pswd='   $pswd
             
.  '&db='     $db
             
.  '&sql='    $sql2
             
.  '&format=' $format;

//      $data = file_get_contents($url);                //Retrieve the URL - Call the Web Service - No longer workds for https

        
$c curl_init($url);
        
curl_setopt($cCURLOPT_RETURNTRANSFER1);
        
$data curl_exec($c);
        
curl_close($c);


//      print($data);                                   //for debug only
}

//------------------------------------------------------------------------------------------------------
function display()
{       
    global 
$user$pswd$db$sql$format$show$msg$data;       

    include 
"include.php";                              //show the code if requested
?>

    <div id="Layer1">
    <form method="POST" action="<?php echo $_SERVER[PHP_SELF?>" >

    <fieldset>
    <br />
    <table>
    <tr><td><b>User     <td> <input type="text"     name="user" size="20" value="<?php echo $user $user 'demo'?>" /> 
    <tr><td><b>Password <td> <input type="password" name="pswd" size="20" value="<?php echo $pswd $pswd '';     ?>" />
    <tr><td><b>Database <td> <input type="text"     name="db"   size="20" value="<?php echo $db   $db   'demo'?>" /> 
    <tr><td><b>SQL      <td> <textarea cols=70 rows=5 name="sql"><?php echo $sql $sql 'show tables'?></textarea> 
    <tr><td>            <td><input type="submit" value=" EXECUTE " />  &nbsp;&nbsp;
                            <select name=format>
                                 <option value=json <?php echo $format=='json' 'SELECTED' ''?> > JSON </option>
                                 <option value=xml  <?php echo $format=='xml'  'SELECTED' ''?> > XML  </option>
                            </select>
    &nbsp;&nbsp;
    Show returned json/xml <input type=checkbox name="show" <?php echo $show 'CHECKED' '' ?> />
    </tr>
    <tr><td><td><br><font color='red'> <?php echo $msg?> </font></td>
    </table>
    </fieldset>
    </form>

<?php 
    
if ($show)                                          //if requesting to show JSON or XML
    
{
        
$data htmlentities($data);
        print 
"<pre>$data</pre>";
        return;
    }

    if (
$format == 'json')
    {
        
$array json_decode($data);                    //convert JSON into a PHP array
        
$row1  $array[0];
    }
    if (
$format == 'xml')
    {
        
$array = new SimpleXMLElement($data);           //convert XML into a PHP object

        
$row1  $array->row[0];
        if (!
$row1
            
$row1 $array->error
    }

    print 
"<table border=1> \n";

    print 
"<tr bgcolor=tan>";                           //print the headers  
    
foreach($row1 as $key => $value)
        print 
"<th> $key </th>";
    print 
"</tr> \n";  

    foreach(
$array as $row => $array2)                  //print the content
    
{
        print 
"<tr bgcolor=white>";
        foreach(
$array2 as $key => $value)
            print 
"<td> $value </td>";
        print 
"</tr> \n";
    }
    print 
"</table> \n";
}

//------------------------------------------------------------------------------------------------------
?>

    </div>
</body>
</html>