openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

client.php (4209B)


      1 <html>
      2 <head>
      3 <title>OpenRat API-Client</title>
      4 </head>
      5 <body>
      6 <h1>OpenRat API-Client</h1>
      7 <h2>Request</h2>
      8 <form action="<?php echo $_SERVER['SCRIPT_NAME'] ?>">
      9 <table>
     10 <tr>
     11 <th>Parameter</th><th>Value</th>
     12 </tr>
     13 <?php for( $i=1; $i<=10; $i++ ) { ?>
     14 <tr>
     15 <td><input name="param<?php echo $i ?>" value="<?php echo $_REQUEST['param'.$i] ?>"></td>
     16 <td><input name="value<?php echo $i ?>" value="<?php echo htmlentities($_REQUEST['value'.$i]) ?>" size="50"></td>
     17 <!-- 
     18 <td><textarea rows="3" cols="50" name="value<?php echo $i ?>"><?php echo htmlentities($_REQUEST['value'.$i]) ?></textarea></td>
     19  -->
     20 </tr>
     21 <?php } ?>
     22 </table><br>
     23 <select name="type">
     24 <?php foreach( array('text/html','application/json','application/xml') as $type ) { ?>
     25 <option value="<?php echo $type ?>" <?php echo ($_REQUEST['type']==$type)?'selected':'' ?>><?php echo $type ?></option>
     26 <?php } ?>
     27 </select>
     28 
     29 <select name="method">
     30 <option value="GET" <?php echo ($_REQUEST['method']=='GET')?'selected':'' ?>>GET</option>
     31 <option value="POST" <?php echo ($_REQUEST['method']=='POST')?'selected':'' ?>>POST</option>
     32 </select>
     33 
     34 <input type="submit">
     35 </form>
     36 <hr>
     37 <h2>Response</h2>
     38 <strong>
     39 <?php if ( !empty($_REQUEST['param1']) ) {
     40 
     41 		$error  = '';
     42 		$status = '';
     43 
     44 		$errno  = 0;
     45 		$errstr = '';
     46 
     47 		$host   = $_SERVER['SERVER_ADDR'];
     48 		$port   = $_SERVER['SERVER_PORT'];
     49 		$path   = substr($_SERVER['SCRIPT_NAME'],0,-22).'/dispatcher.php';
     50 		
     51 		$method = $_REQUEST['method'];
     52 		if	( empty($method))
     53 			$method='GET';
     54 
     55 		// Die Funktion fsockopen() erwartet eine Protokollangabe (bei TCP optional, bei SSL notwendig).
     56 		if	( $port == '443' )
     57 			$prx_proto = 'ssl://'; // SSL
     58 		else
     59 			$prx_proto = 'tcp://'; // Default
     60 			
     61 		$fp = fsockopen ($prx_proto.$host,$port, $errno, $errstr, 30);
     62 
     63 		if	( !$fp || !is_resource($fp) )
     64 		{
     65 			echo "Connection refused: '".$prx_proto.$host.':'.$port." - $errstr ($errno)";
     66 		}
     67 		else
     68 		{
     69 			$lb = "\r\n";
     70 			$http_get = $path;
     71 
     72 			$parameterString = '';
     73 
     74 			for( $i = 1;$i<=10;$i++)
     75 			{
     76 				if	(!empty($_REQUEST['param'.$i]))
     77 				{
     78 					if	( strlen($parameterString) > 0)
     79 						$parameterString .= '&';
     80 					elseif	( $withPraefixQuestionMark )
     81 						$parameterString .= '?';
     82 						
     83 					$parameterString .= urlencode($_REQUEST['param'.$i]) . '=' .urlencode($_REQUEST['value'.$i]);
     84 				}
     85 			}
     86 			
     87 			if	( $method == 'GET')
     88 				if	( !empty($parameterString) )
     89 					$http_get .= '?'.$parameterString;
     90 
     91 			if	( $method == 'POST' )
     92 			{
     93 				$header[] = 'Content-Type: application/x-www-form-urlencoded';
     94 				$header[] = 'Content-Length: '.strlen($parameterString);
     95 			}
     96 					
     97 			$header[] = 'Host: '.$host;
     98 			$header[] = 'Accept: '.$_REQUEST['type'];
     99 			$request_header = array( $method.' '.$http_get.' HTTP/1.0') + $header;
    100 			$http_request = implode($lb,$request_header).$lb.$lb;
    101 			
    102 			if	( $method == 'POST' )
    103 				$http_request .= $parameterString;
    104 
    105 			if (!is_resource($fp)) {
    106 				$error = 'Connection lost after connect: '.$prx_proto.$host.':'.$port;
    107 				return false;
    108 			}
    109 			fputs($fp, $http_request); // Die HTTP-Anfrage zum Server senden.
    110 
    111 			// Jetzt erfolgt das Auslesen der HTTP-Antwort.
    112 			$isHeader = true;
    113 
    114 			// RFC 1945 (Section 6.1) schreibt als Statuszeile folgendes Format vor
    115 			// "HTTP/" 1*DIGIT "." 1*DIGIT SP 3DIGIT SP
    116 			if (!is_resource($fp)) {
    117 				echo 'Connection lost during transfer: '.$host.':'.$port;
    118 			}
    119 			elseif (!feof($fp)) {
    120 				$line = fgets($fp,1028);
    121 				$status = substr($line,9,3);
    122 			}
    123 			else
    124 			{
    125 				echo 'Unexpected EOF while reading HTTP-Response';
    126 			}
    127 			
    128 			while (!feof($fp)) {
    129 				$line = fgets($fp,1028);
    130 				if	( $isHeader && trim($line)=='' ) // Leerzeile nach Header.
    131 				{
    132 					$isHeader = false;
    133 				}
    134 				elseif( $isHeader )
    135 				{
    136 					list($headerName,$headerValue) = explode(': ',$line) + array(1=>'');
    137 					$responseHeader[$headerName] = trim($headerValue);
    138 				}
    139 				else
    140 				{
    141 					$body .= $line;
    142 				}
    143 			}
    144 			fclose($fp); // Verbindung brav schlie�en.
    145 			$response = $body;
    146 
    147 			// 301 Moved Permanently
    148 			// 302 Moved Temporarily
    149 			echo '<span style="background-color:'.($status=='200'?'green':'red').'">HTTP-Status '.$status.'</span>';
    150 		}
    151 	?>
    152 	</strong>
    153 <pre><?php echo htmlentities($response) ?></pre>
    154 <?php } ?>
    155 </body>
    156 </html>