Location: PHPKode > scripts > Handle remote data with PHP 5 and REST Web service > handle-remote-data-with-php-5-and-rest-web-service/php_publ071008.htm
<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>)&nbsp;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">&nbsp;&nbsp;$opts = array(<br>
						</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;'http'=&gt;array('method'=&gt;&quot;POST&quot;,'content'=&gt;$data,<br>
						</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;'header'=&gt;&quot;Content-type: application/x-www-form-urlencoded\r\n&quot;)<br>
						</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;);<br>
						</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp;&nbsp;$context = stream_context_create($opts);<br>
						</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp;&nbsp;return file_get_contents($url, FALSE, $context);<br>
						</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp;&nbsp;}<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">&nbsp;&nbsp;$this-&gt;user= $user;<br>
						</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp;&nbsp;$this-&gt;pw = $pw;<br>
						</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp;&nbsp;$this-&gt;account = $account;<br>
						</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp;&nbsp;$this-&gt;ipaddress = $ipaddress;<br>
						</font></p>
					<p><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp;&nbsp;$data = 'action=doadd&amp;user='.$this-&gt;user.'&amp;pw='.$this-&gt;pw.'&amp;account='.$this-&gt;account.'&amp;ipaddress='.$this-&gt;ipaddress;<br>
						</font></p>
					<p><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp;&nbsp;$res = $this-&gt;make_post_request($this-&gt;url,$data);<br>
						</font></p>
					<p><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp;&nbsp;if($res === '') {<br>
						</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp;&nbsp;&nbsp;&nbsp;return false;</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp;&nbsp;}<br>
						</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp;&nbsp;else&nbsp;&nbsp;{<br>
						</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp;&nbsp;&nbsp;&nbsp;return $res;<br>
						</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp;&nbsp;}</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">&nbsp; &nbsp;if ($this-&gt;doc = $this-&gt;getResource()) {<br>
						</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp; &nbsp;&nbsp; &nbsp;$this-&gt;doc-&gt;formatOutput = true;<br>
						</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp; &nbsp;&nbsp; &nbsp;$this-&gt;doc-&gt;preserveWhiteSpace = false;<br>
						</font></p>
					<p><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp; &nbsp;&nbsp; &nbsp;$xpath = new DOMXPath($this-&gt;doc);<br>
						</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp; &nbsp;&nbsp; &nbsp;$query = '/school/courses/course/descr[@nr = '.$id.']';<br>
						</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp; &nbsp;&nbsp; &nbsp;$entries = $xpath-&gt;query($query);<br>
						</font></p>
					<p><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp; &nbsp;&nbsp; &nbsp;$newtxt = $this-&gt;doc-&gt;createTextNode($value);<br>
						</font></p>
					<p><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp; &nbsp;&nbsp; &nbsp;foreach ($entries as $entry) {<br>
						</font></p>
					<p><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if($entry) {<br>
						</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;foreach($entry-&gt;childNodes as $cnode) {<br>
						</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if($cnode-&gt;nodeType == 3) {<br>
						</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if($entry-&gt;nodeName == $fields) {<br>
						</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;$cnode-&gt;parentNode-&gt;replaceChild($newtxt, $cnode);<br>
						</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;$valchar = &quot;sY06CSMqwoJiX&quot;;<br>
						</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}</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">&nbsp; &nbsp;if ($this-&gt;doc-&gt;save($this-&gt;resource_filename)) {<br>
						</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp; &nbsp;&nbsp; &nbsp;return $this-&gt;doc-&gt;saveXML().$valchar;<br>
						</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp; &nbsp;}</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">&nbsp; &nbsp;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%">&nbsp;<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">&nbsp; &nbsp;private $bpw;<br>
						</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp; &nbsp;private $zpw;<br>
						</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp; &nbsp;private $tpw;<br>
						</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp; &nbsp;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">&nbsp; &nbsp;$bpw = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';<br>
						</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp; &nbsp;$zpw = rand(1,13000000);<br>
						</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp; &nbsp;$tpw = str_shuffle($zpw.$bpw);<br>
						</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp; &nbsp;$rpw = substr(($tpw),0,13); <br>
							<br>
						</font><font size="1" color="blue" face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular">&nbsp; &nbsp;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>
Return current item: Handle remote data with PHP 5 and REST Web service