Handle remote data with PHP 5 and REST Web service

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 below). 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.

The script snippets you see here (the complete script is downloadable) 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). Best regards Claudio Biesele


Part of the PHP AddUser Class Code (client):

protected function make_post_request($url, $data) {

  $opts = array(
         'http'=>array('method'=>"POST",'content'=>$data,
         'header'=>"Content-type: application/x-www-form-urlencoded\r\n")
         );
  $context = stream_context_create($opts);
  return file_get_contents($url, FALSE, $context);
  }

public function addUserLogin($user,$pw,$account,$ipaddress) {

  $this->user= $user;
  $this->pw = $pw;
  $this->account = $account;
  $this->ipaddress = $ipaddress;

  $data = 'action=doadd&user='.$this->user.'&pw='.$this->pw.'&account='.$this->account.'&ipaddress='.$this->ipaddress;

  $res = $this->make_post_request($this->url,$data);

  if($res === '') {
    return false;  }
  else  {
    return $res;
  }} 


Part of the XML Update Handler Class Code (server):

public function updateResource($id, $value, $fields) {

   if ($this->doc = $this->getResource()) {
      $this->doc->formatOutput = true;
      $this->doc->preserveWhiteSpace = false;

      $xpath = new DOMXPath($this->doc);
      $query = '/school/courses/course/descr[@nr = '.$id.']';
      $entries = $xpath->query($query);

      $newtxt = $this->doc->createTextNode($value);

      foreach ($entries as $entry) {

         if($entry) {
            foreach($entry->childNodes as $cnode) {
               if($cnode->nodeType == 3) {
                  if($entry->nodeName == $fields) {
                      $cnode->parentNode->replaceChild($newtxt, $cnode);
                            $valchar = "sY06CSMqwoJiX";
         }}}}}

   if ($this->doc->save($this->resource_filename)) {
      return $this->doc->saveXML().$valchar;
   } }

   return get_error();
}


 
The PHP Class to generate ids or passwords (client):

class clientIdGenerator {

   private $bpw;
   private $zpw;
   private $tpw;
   private $rpw;

public function createAccountId() {

   $bpw = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
   $zpw = rand(1,13000000);
   $tpw = str_shuffle($zpw.$bpw);
   $rpw = substr(($tpw),0,13);

   return $rpw;
}}


Picture RESTWebservice001.jpg
1. View: Before the user can login he has to set the account id given by an id generator class.
Picture RESTWebservice002.jpg
2. View: After the login xml and data table handling are at your service.
Picture RESTWebservice003.jpg
3. View: This form does all the work to handle the remote xml file, if its read- and writable.
Picture RESTWebservice004.jpg
4. View: In this part of the application user data on remote server can be handled easily.
Picture RESTWebservice005.jpg
5. View: When new user is to be added IP address and account id are taken automatically.
Picture RESTWebservice006.jpg
6. View: Content for the dropdown menu is received remote and prepared for presentation.
Download the full script at www.fastproject.ch. The downloaded script is free from Copyright restrictions, Zurich, 16th of November 2015.
Donate with PayPal:
placeholder