Simple WebService module with PHP.

 

<?php

 

// PHP 5.3.x for Windows can be downloaded here: http://windows.php.net

// WAMPServer can be downloaded here: http://www.wampserver.com/en/

//////////////////////////////////////////////////////////////////////////////////

// Author: SYO

// Proof Of Concept demo-ing a simple webservice module (written with PHP) and

// HttpDownload.vbs (ScriptEZ.API based) client which consumes this Webservice.

//

// Purpose: 

// ---------

//    1) generating random X occurrences from within a loop where counter is provided

//       by external client app or script ( here, it is HttpDownload.vbs )

//    2) outputting those values in HTML table format or flat ASCII lines

//

// How to see demo from command line ?

// (1) GET method:

//   c:\>httpdownload <website path>/Requester.php?loop=100&output=html c:\test.htm

//   c:\>httpdownload <website path>/Requester.php?loop=100&output=ascii c:\test.txt

// (2) POST method:

//   WS-TimeReader.vbs

////////////////////////////////////////////////////////////////////////////////////

 

/* Disable displaying errors */

ini_set('display_errors','off');  

 

$header_size = 0;

if( isset($_GET['header']) )

{

    $header_size = $_GET['header'];

}

elseif( isset($_POST['header']) )

{

    $header_size = $_POST['header'];

}

 

if( isset($_GET['loop']) || isset($_POST['loop']) )

{

   $loop = (isset($_GET['loop']) ? $_GET['loop'] : $_POST['loop']) ;

 

   if( (!empty($_GET['output'])  && $_GET['output']  == 'html') ||

       (!empty($_POST['output']) && $_POST['output'] == 'html') )

   {

       // generating HTML table of data

       echo headerPrefixing("<h2>Loop counter:" . $loop . "<h2>\r\n",$header_size);

       echo headerPrefixing("<table border=1>

                              <tr>

                               <th><h3>Order number</h3></th>

                               <th align='left'><h3>Random X occurrences</h3></th>

                              </tr>\r\n",$header_size);

       for($i=1; $i<=$loop; $i++)

       {

         $alea_string = jobToDo(&$r);

         echo headerPrefixing("<tr><td>$i</td><td>($r) $alea_string</td></tr>\r\n",$header_size);

       }

       echo headerPrefixing("</table>\r\n",$header_size);

   }

   elseif( (empty($_GET['output'])  || $_GET['output']  == 'ascii') ||

           (empty($_POST['output']) || $_POST['output'] == 'ascii'))

   {

       // generating flat ASCII lines of data

       echo headerPrefixing("Loop counter:" . $loop . "\r\n",$header_size);

       echo headerPrefixing("Order number\r\n",$header_size);

       for($i=1; $i<=$loop; $i++)

       {

        

         $alea_string = jobToDo(&$r);

         echo headerPrefixing("$i:($r) $alea_string\r\n",$header_size);

       }

   }

}

elseif( isset($_GET['time']) || isset($_POST['time']))

{

     // output info of time and some sys info

     $load='';

     date_default_timezone_set("Europe/Paris");

     try

     {

         if(function_exists("memory_get_usage") == true )

         {

             $mem = memory_get_usage(true);

         }

         else

         {

             $mem="?";

         }

     }

     catch(Exception $e)

     {

         $mem="?";

     }

    

     try

     {

         if(function_exists("apache_get_version") == true)

         {

             $webserver_ver = apache_get_version();

         }

         else

         {

             $webserver_ver = PHP_OS;

         }

     }

     catch(Exception $e)

     {

         $webserver_ver = PHP_OS;

     }

        

     try

     {

       if(function_exists("sys_getloadavg") == true)

       {

            $load = sys_getloadavg();

       }

     }

     catch(Exception $e)

     {

       $load[0]= $e->getMessage();

     }

    

     if(is_array($load))

     {

         echo headerPrefixing(date("F j, Y, G:i:s")." / ". $webserver_ver ." | ".$mem. " Bytes | load: ". $load[0] ."%",$header_size);

     }

     else

     {

         echo headerPrefixing(date("F j, Y, G:i:s")." / ". $webserver_ver ." | ".$mem. " Bytes",$header_size);

     }

}

else

{

  echo ". Unknown parameter(s), please use on the following syntaxes:\r\n".

       ". (1)" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . $_SERVER['PHP_SELF'] .

                                                        "?loop=<integer value>&output=html\r\n" .

       ". (2)" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . $_SERVER['PHP_SELF'] .

                                                        "?loop=<integer value>&output=ascii\r\n";

}

 

// This is the job to do by this simple Webservice

function jobToDo(&$r)

{

        $r = rand(1,8192); // randomize from 1 to 8192 bytes

        return str_pad("",$r,"X");

}

 

function headerPrefixing($data, $hdrSize = 0)

{   

   if($hdrSize == 0)

   {

      // no header area

      return $data;

   }

   else

   {

      // with simple header area where length is in bytes + payload        

      return sprintf("%0$hdrSize"."d%s",strlen($data),$data);

   }

}

?>