Changes between Version 1 and Version 2 of calculator_service

Show
Ignore:
Timestamp:
10/31/07 16:51:06 (6 years ago)
Author:
hduong
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • calculator_service

    v1 v2  
    11 
    22 
    3 An observer of unicorn is simply a webservice whose the reponse page validate the observer-reponse schema 
     3An observer of unicorn is simply a webservice whose the reponse page validate the [wiki:observer-reponse] schema (output contract). In this part, we're going to make a calculator webservice which respect this output contract. 
     4[[BR]] 
    45 
    56 
     7== caculator.php == 
     8 
     9{{{ 
     10<?php header('Content-type: application/xml'); ?> 
     11<?php 
     12$resource = fopen($_GET['uri'], 'r'); 
     13 
     14$line = 0; 
     15 
     16$errors = array(); 
     17$results = array(); 
     18 
     19while(!feof($resource)) { 
     20    $op = trim(fgets($resource)); 
     21    $line++; 
     22    if($op !== '') { 
     23        if(preg_match('/^[0-9*-+()\/]+$/', $op)) { 
     24            ob_start(); 
     25            $result = eval("return " . $op . ';'); 
     26            $out = trim(ob_get_contents()); 
     27            ob_end_clean(); 
     28            if($out !== '') { 
     29                $errors[$line] = array($op, $out); 
     30            } 
     31            else { 
     32                if($_GET['x2']) $result *= 2; 
     33                if($_GET['xn']) $result *= $_GET['xn']; 
     34                $results[$line] = array($op, $result); 
     35            } 
     36        } 
     37        else { 
     38            $errors[$line] = array($op, "Forbidden characters"); 
     39        } 
     40    } 
     41} 
     42 
     43echo '<?xml version="1.0" encoding="UTF-8"?>';  
     44?> 
     45<observationresponse xmlns="http://www.w3.org/unicorn/observationresponse" 
     46                     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     47                     xsi:schemaLocation="http://www.w3.org/QA/2006/obs_framework/ns/observation/  http://www.w3.org/QA/2006/obs_framework/response/observer-response.xsd"> 
     48  <uri><?php echo $_GET['uri']; ?></uri> 
     49  <passed><?php echo (count($errors) === 0 ? 'true' : 'false'); ?></passed> 
     50  <result> 
     51    <errors> 
     52      <errorcount><?php echo count($errors); ?></errorcount> 
     53      <errorlist> 
     54        <uri><?php echo $_GET['uri']; ?></uri> 
     55        <?php foreach($errors as $line => $val): ?> 
     56        <error> 
     57          <line><?php echo $line; ?></line> 
     58          <message><?php echo $val[1]; ?></message> 
     59          <context><?php echo $val[0]; ?></context>      
     60        </error> 
     61        <?php endforeach; ?> 
     62      </errorlist> 
     63    </errors> 
     64    <informations> 
     65      <infocount><?php echo count($results); ?></infocount> 
     66      <infolist> 
     67        <uri><?php echo $_GET['uri']; ?></uri> 
     68        <?php foreach($results as $line => $val): ?> 
     69        <info> 
     70          <line><?php echo $line; ?></line> 
     71          <message><?php echo $val[0]; ?> = <?php echo $val[1]; ?></message> 
     72        </info> 
     73        <?php endforeach; ?> 
     74      </infolist> 
     75    </informations> 
     76  </result> 
     77</observationresponse> 
     78 
     79}}} 
    680 
    781 
     82== Explication == 
     83 
     84 * Input: the parmater uri = path to your text file what is accessible by your server 
     85 * Output: a observer-reponse which expose all possible calculation result in the file 
     86 * Test: http://yourhost/caculator.php?uri=http://xyz/a.txt 
     87 
     88