miniblog

Unnamed repository; edit this file 'description' to name the repository.
git clone http://git.code.weiherhei.de/miniblog.git
Log | Files | Refs

OpenratClient.php (3003B)


      1 <?php
      2 
      3 class OpenratClient
      4 {
      5 	public $error  = '';
      6 	public $status = '';
      7 
      8 	public $host;
      9 	public $port;
     10 	public $path;
     11 	public $method = 'GET';
     12 	public $response;
     13 	public $parameter = array();
     14 	public $type = "text/json";
     15 	public $cookie = "";
     16 	
     17 	public function request()
     18 	{
     19 		$errno  = 0;
     20 		$errstr = '';
     21 
     22 		// Die Funktion fsockopen() erwartet eine Protokollangabe (bei TCP optional, bei SSL notwendig).
     23 		if	( $this->port == '443' )
     24 			$prx_proto = 'ssl://'; // SSL
     25 		else
     26 			$prx_proto = 'tcp://'; // Default
     27 			
     28 		$fp = fsockopen($prx_proto.$this->host,$this->port, $errno, $errstr, 30);
     29 
     30 		if	( !$fp || !is_resource($fp) )
     31 		{
     32 			$this->error = "Connection refused: '".$prx_proto.$host.':'.$port." - $errstr ($errno)";
     33 		}
     34 		else
     35 		{
     36 			$lb = "\r\n";
     37 			$http_get = $this->path;
     38 
     39 			$parameterString = '';
     40 
     41 			foreach( $this->parameter as $pkey=>$pvalue)
     42 			{
     43 				if	( strlen($parameterString) > 0)
     44 					$parameterString .= '&';
     45 					
     46 				$parameterString .= urlencode($pkey) . '=' .urlencode($pvalue);
     47 			}
     48 			//print_r($parameterString);
     49 			
     50 			if	( $this->method == 'GET')
     51 				if	( !empty($parameterString) )
     52 					$http_get .= '?'.$parameterString;
     53 
     54 			if	( $this->method == 'POST' )
     55 			{
     56 				$header[] = 'Content-Type: application/x-www-form-urlencoded';
     57 				$header[] = 'Content-Length: '.strlen($parameterString);
     58 			}
     59 					
     60 			$header[] = 'Host: '.$this->host;
     61 			$header[] = 'Accept: '.$this->type;
     62 			$header[] = 'Cookie: '.$this->cookie;
     63 			$request_header = array_merge(array( $this->method.' '.$http_get.' HTTP/1.0'),$header);
     64 				//print_r($request_header);
     65 			$http_request = implode($lb,$request_header).$lb.$lb;
     66 			
     67 			
     68 			if	( $this->method == 'POST' )
     69 				$http_request .= $parameterString;
     70 				
     71 			//echo "<textarea>".htmlentities($http_request)."</textarea>";
     72 
     73 			if (!is_resource($fp)) {
     74 				$error = 'Connection lost after connect: '.$prx_proto.$host.':'.$port;
     75 				return false;
     76 			}
     77 			fputs($fp, $http_request); // Die HTTP-Anfrage zum Server senden.
     78 
     79 			// Jetzt erfolgt das Auslesen der HTTP-Antwort.
     80 			$isHeader = true;
     81 
     82 			// RFC 1945 (Section 6.1) schreibt als Statuszeile folgendes Format vor
     83 			// "HTTP/" 1*DIGIT "." 1*DIGIT SP 3DIGIT SP
     84 			if (!is_resource($fp)) {
     85 				echo 'Connection lost during transfer: '.$host.':'.$port;
     86 			}
     87 			elseif (!feof($fp)) {
     88 				$line = fgets($fp,1028);
     89 				$this->status = substr($line,9,3);
     90 			}
     91 			else
     92 			{
     93 				echo 'Unexpected EOF while reading HTTP-Response';
     94 			}
     95 			
     96 			$body = '';
     97 			
     98 			while (!feof($fp)) {
     99 				$line = fgets($fp,1028);
    100 				if	( $isHeader && trim($line)=='' ) // Leerzeile nach Header.
    101 				{
    102 					$isHeader = false;
    103 				}
    104 				elseif( $isHeader )
    105 				{
    106 					list($headerName,$headerValue) = explode(': ',$line) + array(1=>'');
    107 					//if	( $headerName == 'Set-Cookie' )
    108 					//	$this->cookie = $headerValue;
    109 					$responseHeader[$headerName] = trim($headerValue);
    110 				}
    111 				else
    112 				{
    113 					$body .= $line;
    114 				}
    115 			}
    116 			fclose($fp); // Verbindung brav schliessen.
    117 			$this->response = $body;
    118 		}
    119 	}
    120 }
    121 
    122 ?>