[ Index ]

PHP Cross Reference of JPSpan 0.4 (beta)

title

Body

[close]

/examples/ -> testclient.php (source)

   1  <?php
   2  // $Id: testclient.php,v 1.3 2004/11/22 11:06:25 harryf Exp $
   3  require_once  '../JPSpan.php';
   4  require_once JPSPAN . 'Include.php';
   5  JPSpan_Include_Register('encode/php.js');
   6  JPSpan_Include_Register('encode/xml.js');
   7  JPSpan_Include_Register('request/get.js');
   8  JPSpan_Include_Register('request/post.js');
   9  JPSpan_Include_Register('request/rawpost.js');
  10  JPSpan_Include_Register('util/data.js');
  11  
  12  header( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); 
  13  header( "Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" ); 
  14  header( "Cache-Control: no-cache, must-revalidate" ); 
  15  header( "Pragma: no-cache" );
  16  
  17  function path() {
  18      $basePath = explode('/',$_SERVER['SCRIPT_NAME']);
  19      $script = array_pop($basePath);
  20      $basePath = implode('/',$basePath);
  21      if ( isset($_SERVER['HTTPS']) ) {
  22          $scheme = 'https';
  23      } else {
  24          $scheme = 'http';
  25      }
  26      echo $scheme.'://'.$_SERVER['SERVER_NAME'].$basePath;
  27  }
  28  
  29  ?>
  30  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  31  <html>
  32  <head>
  33  <title> HttpClient Test Page </title>
  34  <style type="text/css">
  35  <!--
  36  body {
  37      margin:0px;
  38      padding:0px;
  39      font-family:verdana, arial, helvetica, sans-serif;
  40      color:#333;
  41      background-color:white;
  42      }
  43  h1 {
  44      margin:0px 0px 0px 0px;
  45      padding:0px;
  46      font-size:28px;
  47      line-height:28px;
  48      font-weight:900;
  49      color:#ccc;
  50      }
  51  h2 {
  52      margin:0px 0px 0px 0px;
  53      padding:0px;
  54      font-size:18px;
  55      line-height:18px;
  56      font-weight:500;
  57      line-height:24px;
  58      }
  59  p {
  60      font:11px/20px verdana, arial, helvetica, sans-serif;
  61      margin:0px 0px 16px 0px;
  62      padding:0px;
  63      }
  64  #Content>p {margin:0px;}
  65  #Content>p+p {text-indent:30px;}
  66  
  67  input, select {
  68      font:11px/20px verdana, arial, helvetica, sans-serif;
  69      margin:0px 0px 0px 0px;
  70      padding:0px;
  71      }
  72  
  73  a {
  74      color:#09c;
  75      font-size:11px;
  76      text-decoration:none;
  77      font-weight:600;
  78      font-family:verdana, arial, helvetica, sans-serif;
  79      }
  80  a:link {color:#09c;}
  81  a:visited {color:#07a;}
  82  a:hover {background-color:#eee;}
  83  #Header {
  84      margin:50px 0px 10px 0px;
  85      padding:17px 0px 0px 20px;
  86      /* For IE5/Win's benefit height = [correct height] + [top padding] + [top and bottom border widths] */
  87      height:33px; /* 14px + 17px + 2px = 33px */
  88      line-height:11px;
  89      voice-family: "\"}\"";
  90      voice-family:inherit;
  91      height:14px; /* the correct height */
  92      }
  93  body>#Header {height:14px;}
  94  #Content {
  95      margin:0px 50px 50px 300px;
  96      padding:10px;
  97      }
  98  
  99  #Menu {
 100      position:absolute;
 101      top:100px;
 102      left:20px;
 103      width:172px;
 104      padding:10px;
 105      line-height:17px;
 106      font-size:11px;
 107      voice-family: "\"}\"";
 108      voice-family:inherit;
 109      width:250px;
 110      }
 111  body>#Menu {width:250px;}
 112  -->
 113  </style>
 114  <script type="text/javascript">
 115  <?php JPSpan_Includes_Display(); ?>
 116  
 117  var serverUrl = "<?php path();?>/printrresponse.php";
 118  var requestUrl = "<?php path();?>/printrresponse.php?rencoding=xml";
 119  var encoder = new JPSpan_Encode_Xml();
 120  
 121  //-------------------------------------------------------------------------
 122  // Utility functions for this example
 123  function echo(string) {
 124      document.getElementById("results").innerHTML += string;
 125  }
 126  
 127  function clear() {
 128      document.getElementById("results").innerHTML = "";
 129  }
 130  
 131  function getTimeOut() {
 132      timeout = parseInt(document.getElementById("timeout").value);
 133      if ( !isNaN(timeout) ) {
 134          return timeout;
 135      }
 136      return 20000;
 137  }
 138  
 139  function setREncoding(value) {
 140      requestUrl = serverUrl + '?rencoding='+value;
 141      if ( value == 'php' ) {
 142          encoder = new JPSpan_Encode_PHP();
 143      } else {
 144          encoder = new JPSpan_Encode_Xml();
 145      }
 146  }
 147  
 148  function var_dump(data) {
 149      var Data = new JPSpan_Util_Data();
 150      return Data.dump(data);
 151  }
 152  //-------------------------------------------------------------------------
 153  // ASYNC stuff
 154  //-------------------------------------------------------------------------
 155  function asyncPost() {
 156      var c = new JPSpan_HttpClient();
 157      var r = new JPSpan_Request_Post(encoder);
 158      r.timeout = getTimeOut();
 159      
 160      r.serverurl = requestUrl;
 161      r.addArg('method','POST');
 162      r.addArg('x','foo');
 163      r.addArg('y','bar');
 164      r.addArg('z',new Array(1,2,3));
 165      c.asyncCall(r, BasicHandler);
 166  }
 167  
 168  function asyncGet() {
 169      var c = new JPSpan_HttpClient();
 170      var r = new JPSpan_Request_Get(encoder);
 171      r.timeout = getTimeOut();
 172      r.serverurl = requestUrl;
 173      r.addArg('method','GET');
 174      r.addArg('x','foo');
 175      r.addArg('y','bar');
 176      r.addArg('z',new Array(1,2,3));
 177      c.asyncCall(r, BasicHandler);
 178  }
 179  
 180  function asyncRawPost() {
 181      var c = new JPSpan_HttpClient();
 182      var r = new JPSpan_Request_RawPost(encoder);
 183      r.timeout = getTimeOut();
 184      r.serverurl = requestUrl;
 185      r.addArg('method','RawPOST');
 186      r.addArg('x','foo');
 187      r.addArg('y','bar');
 188      r.addArg('z',new Array(1,2,3));
 189      c.asyncCall(r, BasicHandler);
 190  }
 191  
 192  var BasicHandler = {
 193      onLoad: function(result) {
 194          echo("<pre>"+var_dump(result)+"</pre>");
 195      },
 196      onError: function(e) {
 197          alert(e.name+': '+e.message+' (JPSpan Code: '+e.code+')');
 198      }
 199  }
 200  
 201  var EvalHandler = {
 202      onLoad: function(result) {
 203          try {
 204              result = eval(result);
 205              result = result();
 206              echo("<pre>"+var_dump(result)+"</pre>");
 207          } catch(e) {
 208              this.onError(e);
 209          }
 210      },
 211      onError: function(e) {
 212          alert(e.name+': '+e.message+"\n [File: "+e.file+', line: '+e.line+"]\n (JPSpan Code: "+e.code+')');
 213      }
 214  }
 215  
 216  //-------------------------------------------------------------------------
 217  // SYNC Stuff
 218  //-------------------------------------------------------------------------
 219  function syncPost() {
 220      var c = new JPSpan_HttpClient();
 221      var r = new JPSpan_Request_Post(encoder);
 222      r.serverurl = requestUrl;
 223      r.addArg('method','POST');
 224      r.addArg('x','foo');
 225      r.addArg('y','bar');
 226      r.addArg('z',new Array(1,2,3));
 227      try {
 228          echo('<pre>'+c.call(r)+'</pre>');
 229      } catch ( e ) {
 230          echo (e.message);
 231      }
 232  }
 233  
 234  function syncGet() {
 235      var c = new JPSpan_HttpClient();
 236      var r = new JPSpan_Request_Get(encoder);
 237      r.serverurl = requestUrl;
 238      r.addArg('method','GET');
 239      r.addArg('x','foo');
 240      r.addArg('y','bar');
 241      r.addArg('z',new Array(1,2,3));
 242      try {
 243          echo('<pre>'+c.call(r)+'</pre>');
 244      } catch ( e ) {
 245          echo (e.message);
 246      }
 247  }
 248  
 249  function syncRawPost() {
 250      var c = new JPSpan_HttpClient();
 251      var r = new JPSpan_Request_RawPost(encoder);
 252      r.serverurl = requestUrl;
 253      r.addArg('method','RawPOST');
 254      r.addArg('x','foo');
 255      r.addArg('y','bar');
 256      r.addArg('z',new Array(1,2,3));
 257      try {
 258          echo('<pre>'+c.call(r)+'</pre>');
 259      } catch ( e ) {
 260          echo (e.message);
 261      }
 262  }
 263  //-------------------------------------------------------------------------
 264  
 265  function notFound() {
 266      var c = new JPSpan_HttpClient();
 267      var r = new JPSpan_Request_Get(encoder);
 268      r.serverurl = 'http://localhost/pagenotfound_123567234.html';
 269      c.asyncCall(r,BasicHandler);
 270  }
 271  
 272  function permissionDenied() {
 273      var c = new JPSpan_HttpClient();
 274      var r = new JPSpan_Request_Get(encoder);
 275      r.serverurl = 'http://127.0.0.253/';
 276      try {
 277          c.asyncCall(r,BasicHandler);
 278      } catch(e) {
 279          BasicHandler.onError(e);
 280      }
 281  }
 282  
 283  function callInProgress() {
 284      var c = new JPSpan_HttpClient();
 285  
 286      for (var i=0; i<3; i++ ) {
 287      
 288          var r = new JPSpan_Request_Get(encoder);
 289          
 290          // Interesting... (try commenting then asyncGet then multiAsyncGet)
 291          r.reset();
 292          
 293          r.serverurl = requestUrl;
 294          r.addArg('requestNum','request number: '+i);
 295          try {
 296              c.asyncCall(r, BasicHandler);
 297          } catch (e) {
 298              alert('Request #'+i+' ['+e.code+'] '+e);
 299          }
 300      }
 301  }
 302  
 303  function timeout() {
 304      var c = new JPSpan_HttpClient();
 305      var r = new JPSpan_Request_Get(encoder);
 306      r.timeout = 1;
 307      r.serverurl= requestUrl+'&timeout=3';
 308      r.addArg('method','GET');
 309      r.addArg('x','foo');
 310      r.addArg('y','bar');
 311      r.addArg('z',new Array(1,2,3));
 312      c.asyncCall(r, BasicHandler);
 313  }
 314  
 315  function invalidParamName() {
 316      var c = new JPSpan_HttpClient();
 317      var r = new JPSpan_Request_Get(encoder);
 318      try {
 319          r.addArg('%50','foo');
 320          c.asyncCall(r, BasicHandler);
 321      } catch (e) {
 322          BasicHandler.onError(e);
 323      }
 324  }
 325  
 326  function recursion() {
 327      var c = new JPSpan_HttpClient();
 328      var r = new JPSpan_Request_Get(encoder);
 329      r.serverurl = requestUrl;
 330      var a = new Object();
 331      var b = new Object();
 332      a.b = b;
 333      b.a = a;
 334      try {
 335          r.addArg('a',a);
 336          c.asyncCall(r, BasicHandler);
 337      } catch (e) {
 338          BasicHandler.onError(e);
 339      }
 340  }
 341  //-------------------------------------------------------------------------
 342  function fopen() {
 343      var c = new JPSpan_HttpClient();
 344      var r = new JPSpan_Request_Get(encoder);
 345      r.serverurl = requestUrl+'&error=native';
 346      c.asyncCall(r, EvalHandler);
 347  }
 348  
 349  function notice() {
 350      var c = new JPSpan_HttpClient();
 351      var r = new JPSpan_Request_Get(encoder);
 352      r.serverurl = requestUrl+'&error=notice';
 353      c.asyncCall(r, EvalHandler);
 354  }
 355  
 356  function warning() {
 357      var c = new JPSpan_HttpClient();
 358      var r = new JPSpan_Request_Get(encoder);
 359      r.serverurl = requestUrl+'&error=warning';
 360      c.asyncCall(r, EvalHandler);
 361  }
 362  
 363  function error() {
 364      var c = new JPSpan_HttpClient();
 365      var r = new JPSpan_Request_Get(encoder);
 366      r.serverurl = requestUrl+'&error=error';
 367      c.asyncCall(r, EvalHandler);
 368  }
 369  
 370  function exception() {
 371      var c = new JPSpan_HttpClient();
 372      var r = new JPSpan_Request_Get(encoder);
 373      r.serverurl = requestUrl+'&error=exception';
 374      c.asyncCall(r, EvalHandler);
 375  }
 376  -->
 377  </script>
 378  </head>
 379  <body>
 380  <div id="Header">
 381  <h1><acronym title="Examples of JPSpan_HttpClient, JPSpan_Request_Get, JPSpan_Request_Post and JPSpan_Request_RawPost">Client Test Page</acronym></h1>
 382  </div>
 383  <div id="Content">
 384      <h2>Response from Server</h2>
 385      <div id="results">
 386      </div>
 387  </div>
 388  <div id="Menu">
 389  <acronym title="Clean up the results"><a href="javascript:clear()">clear()</a></acronym><br>
 390  <form>
 391  <acronym title="Encoding for request">Encoding</acronym>: <select onChange="setREncoding(this.options[selectedIndex].value);"><option>xml</option><option>php</option></select>
 392  </form>
 393  <br>
 394  <h2>Async</h2>
 395  <acronym title="Asynchronous HTTP POST request"><a href="javascript:asyncPost()">asyncPost()</a></acronym><br>
 396  <acronym title="Asynchronous HTTP GET request"><a href="javascript:asyncGet()">asyncGet()</a></acronym><br>
 397  <acronym title="Asynchronous raw HTTP POST request"><a href="javascript:asyncRawPost()">asyncRawPost()</a></acronym><br>
 398  <form>
 399  <acronym title="Try adding a sleep() to printrresponse.php to simulate"><label>Timeout [ms]</label></acronym>: <input type="text" id="timeout" value="20000" size="5"><br>
 400  </form>
 401  
 402  <h2>Sync</h2>
 403  <acronym title="Synchronous HTTP POST request"><a href="javascript:syncPost()">syncPost()</a></acronym><br>
 404  <acronym title="Synchronous HTTP GET request"><a href="javascript:syncGet()">syncGet()</a></acronym><br>
 405  <acronym title="Asynchronous raw HTTP POST request"><a href="javascript:syncRawPost()">syncRawPost()</a></acronym><br>
 406  <br>
 407  <h2>Client_Error</h2>
 408  <acronym title="HTTP page not found"><a href="javascript:notFound()">notFound()</a></acronym><br>
 409  <acronym title="Request to 127.0.0.253"><a href="javascript:permissionDenied()">permissionDenied()</a></acronym><br>
 410  <acronym title="Attempt multiple calls to when request already in progress"><a href="javascript:callInProgress()">callInProgress()</a></acronym><br>
 411  <acronym title="Request timed out before response received"><a href="javascript:timeout()">timeout()</a></acronym><br>
 412  <acronym title="Invalid request parameter name"><a href="javascript:invalidParamName()">invalidParamName()</a></acronym><br>
 413  <acronym title="Recursive references in request data"><a href="javascript:recursion()">recursion()</a></acronym><br>
 414  <br>
 415  
 416  <h2>Server_Error</h2>
 417  <acronym title="A native PHP error resulting from failed fopen"><a href="javascript:fopen()">fopen()</a></acronym><br>
 418  <acronym title="PHP E_USER_NOTICE from trigger_error()"><a href="javascript:notice()">notice()</a></acronym><br>
 419  <acronym title="PHP E_USER_WARNING from trigger_error()"><a href="javascript:warning()">warning()</a></acronym><br>
 420  <acronym title="PHP E_USER_ERROR from trigger_error()"><a href="javascript:error()">error()</a></acronym><br>
 421  <acronym title="PHP5 Exception"><a href="javascript:exception()">exception()</a></acronym><br>
 422  </div>
 423  </body>
 424  </html>


Generated: Fri Nov 26 11:42:46 2004 Cross-referenced by PHPXref 0.6