<html>
<head>
<meta http-equiv="Content-Language" content="de-ch">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Handle remote data with PHP 5 and REST Web service</title>
</head>
<body>
<table border="0" width="100%">
<tr>
<td width="100%"><font face="Arial" size="1">This Representational State Transfer (REST) Web service is made of three main parts: the login part, the xml part and the data part (see screenshots <a href="#screenshot">below</a>). The first part concerns about the validation of the user data: username, password, account id and IP address, which are all transferred and returned by the function file_get_contents(): It is supported by the function stream_context_create() which takes care of how the request will be made (for safety reasons POST). The second part (xml part) offers a HTTP-connection to an xml file with internal DTD on a remote server which is client side processed by SimpleXMLElement (from the PHP 5 SPL) for presentation and offers the complete CRUD handling (create, retrieve, update, delete) done by the DOM XML extension on the server side (if xml file is read- and writable!). The third part is simple database handling: special features are MySqli (with the exception of stored procedures the most safe kind of MySql data exchange) and a datagateway to manage further restrictions.<br>
</font>
<p><font face="Arial" size="1">The script snippets you see here (the complete script is <a href="#load">downloadable</a>) are only a small part of the complete application. That's it. The MySql code for the database action and other stuff is all prepared for your convenience (create-table-, css- and xml-file included). <i>Best regards Claudio Biesele</i></font></p>
</td>
</tr>
<tr>
<td width="100%">
<hr>
<p><font face="Arial" size="1"><b>Part of the PHP AddUser Class Code (client):</b></font></p>
<p><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">protected function make_post_request($url, $data) {<br>
</font></p>
<p><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> $opts = array(<br>
</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> 'http'=>array('method'=>"POST",'content'=>$data,<br>
</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> 'header'=>"Content-type: application/x-www-form-urlencoded\r\n")<br>
</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> );<br>
</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> $context = stream_context_create($opts);<br>
</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> return file_get_contents($url, FALSE, $context);<br>
</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> }<br>
</font></p>
<p><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> public function addUserLogin($user,$pw,$account,$ipaddress) {<br>
</font></p>
<p><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> $this->user= $user;<br>
</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> $this->pw = $pw;<br>
</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> $this->account = $account;<br>
</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> $this->ipaddress = $ipaddress;<br>
</font></p>
<p><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> $data = 'action=doadd&user='.$this->user.'&pw='.$this->pw.'&account='.$this->account.'&ipaddress='.$this->ipaddress;<br>
</font></p>
<p><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> $res = $this->make_post_request($this->url,$data);<br>
</font></p>
<p><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> if($res === '') {<br>
</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> return false;</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> }<br>
</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> else {<br>
</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> return $res;<br>
</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> }</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">}</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> </font></p>
</td>
</tr>
<tr>
<td width="100%">
<hr>
<p><font face="Arial" size="1"><b>Part of the XML Update Handler Class Code (server):</b></font></p>
<p><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">public function updateResource($id, $value, $fields) {<br>
</font></p>
<p><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> if ($this->doc = $this->getResource()) {<br>
</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> $this->doc->formatOutput = true;<br>
</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> $this->doc->preserveWhiteSpace = false;<br>
</font></p>
<p><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> $xpath = new DOMXPath($this->doc);<br>
</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> $query = '/school/courses/course/descr[@nr = '.$id.']';<br>
</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> $entries = $xpath->query($query);<br>
</font></p>
<p><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> $newtxt = $this->doc->createTextNode($value);<br>
</font></p>
<p><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> foreach ($entries as $entry) {<br>
</font></p>
<p><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> if($entry) {<br>
</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> foreach($entry->childNodes as $cnode) {<br>
</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> if($cnode->nodeType == 3) {<br>
</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> if($entry->nodeName == $fields) {<br>
</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> $cnode->parentNode->replaceChild($newtxt, $cnode);<br>
</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> $valchar = "sY06CSMqwoJiX";<br>
</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> }</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">}</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">}</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">}</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">}<br>
</font></p>
<p><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> if ($this->doc->save($this->resource_filename)) {<br>
</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> return $this->doc->saveXML().$valchar;<br>
</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> }</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> }</font></p>
<p><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> return get_error();<br>
</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> }</font></p>
</td>
</tr>
<tr>
<td width="100%">
<hr>
</td>
</tr>
<tr>
<td width="100%"> <br>
<font face="Arial" size="1"><b>The PHP Class to generate ids or passwords (client):</b></font>
<p><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">class clientIdGenerator {<br>
</font></p>
<p><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> private $bpw;<br>
</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> private $zpw;<br>
</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> private $tpw;<br>
</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> private $rpw;<br>
</font></p>
<p><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">public function createAccountId() {<br>
</font></p>
<p><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> $bpw = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';<br>
</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> $zpw = rand(1,13000000);<br>
</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> $tpw = str_shuffle($zpw.$bpw);<br>
</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> $rpw = substr(($tpw),0,13); <br>
<br>
</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> return $rpw; <br>
</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"> }</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">}</font><font size="1" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular"><a name="screenshot"></a><br>
</font></p>
</td>
</tr>
<tr>
<td width="100%">
<hr>
</td>
</tr>
<tr>
<td width="100%"><img border="0" src="RESTWebservice001.jpg" width="475" height="275"></td>
</tr>
<tr>
<td width="100%"><font face="Arial" size="1"><b>1. View:</b> Before the user can login he has to set the account id given by an id generator class.</font></td>
</tr>
<tr>
<td width="100%"><img border="0" src="RESTWebservice002.jpg" width="475" height="275"></td>
</tr>
<tr>
<td width="100%"><font face="Arial" size="1"><b>2. View:</b> After the login xml and data table handling are at your service.</font></td>
</tr>
<tr>
<td width="100%"><img border="0" src="RESTWebservice003.jpg" width="475" height="275"></td>
</tr>
<tr>
<td width="100%"><font face="Arial" size="1"><b>3. View:</b> This form does all the work to handle the remote xml file, if its read- and writable.</font></td>
</tr>
<tr>
<td width="100%"><img border="0" src="RESTWebservice004.jpg" width="475" height="275"></td>
</tr>
<tr>
<td width="100%"><font face="Arial" size="1"><b>4. View:</b> In this part of the application user data on remote server can be handled easily.</font></td>
</tr>
<tr>
<td width="100%"><img src="RESTWebservice005.jpg" alt="" height="275" width="475" border="0"></td>
</tr>
<tr>
<td width="100%"><font face="Arial" size="1"><b>5. View:</b> When new user is to be added IP address and account id are taken automatically.</font></td>
</tr>
<tr>
<td width="100%"><img src="RESTWebservice006.jpg" alt="" height="275" width="475" border="0"></td>
</tr>
<tr>
<td width="100%"><font face="Arial" size="1"><b>6. View:</b> Content for the dropdown menu is received remote and prepared for presentation.</font></td>
</tr>
<tr>
<td width="100%"></td>
</tr>
<tr>
<td width="100%"><font face="Arial" size="1">Download the full script at <a href="http://www.fastproject.ch/publikationen/RESTWebService.rar#load" target="_blank">www.fastproject.ch</a>. The downloaded script is free from Copyright restrictions, Zurich, 06th of October 2008.<a name="load"></a></font></td>
</tr>
</table>
</body>
</html>