PageRenderTime 121ms CodeModel.GetById 50ms app.highlight 5ms RepoModel.GetById 46ms app.codeStats 0ms

/examples/jsonrpc/public/services/phpolait/httpwrap.php

http://pyjamas.googlecode.com/
PHP | 112 lines | 59 code | 11 blank | 42 comment | 6 complexity | 05f4b80890cd1093a33175743639a213 MD5 | raw file
  1<?php
  2
  3/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4
  5/**
  6 * Wrapper for the appropriate HTTP post method for the system.
  7 *
  8 *
  9 *
 10 * PHP versions 4 and 5 (not tested under 5)
 11 *
 12 * LICENSE: LGPL
 13 *
 14 * @Release PHP-O-Lait Version 0.5
 15 *
 16 */
 17 
 18/**
 19 * Wraps a CURL object.
 20 * Code from : http://www.faqts.com/knowledge_base/view.phtml/aid/15705/fid/6
 21 * Changed to set request as text/plain, and to work correctly with my data type.
 22 * @access public
 23 */
 24class HttpWrapper_CURL {
 25	function post($URL, $data, $referrer="") {         
 26		 // parsing the given URL
 27		 $URL_Info=parse_url($URL);
 28
 29		 // Building referrer
 30		 if($referrer=="") // if not given use this script as referrer
 31			 $referrer=$_SERVER["REQUEST_URI"];
 32
 33		 $ch = curl_init();
 34		 curl_setopt($ch, CURLOPT_URL, $URL_Info["host"].$URL_Info["path"]);
 35		 curl_setopt($ch, CURLOPT_HEADER, 0);		// Don't return headers
 36		 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);	// Return the result when curl_exec is called
 37		 curl_setopt($ch, CURLOPT_REFERER, $referrer );	// The referrer
 38		 curl_setopt($ch, CURLOPT_POST, 0);	// We're doing a post call
 39		 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);	// Here's the post data
 40		 $result=curl_exec ($ch);
 41		 curl_close ($ch);
 42		 return $result;
 43	}
 44}
 45
 46/**
 47 * Native PHP Http Wrapper.
 48 * Basic code idea from: http://www.faqts.com/knowledge_base/view.phtml/aid/15705/fid/6
 49 * Changed to send request as text/plain, and return only the response body.
 50 *
 51 * @access public
 52 */
 53class HttpWrapper_PHP {
 54	function post($URL, $data, $referrer="") {
 55		if($referrer=="") // if not given use this script as referrer
 56			$referrer=$_SERVER["SCRIPT_URI"];
 57	
 58		$URL_Info=parse_url($URL);
 59		if(!isset($URL_Info["port"]))
 60			$URL_Info["port"]=80;
 61
 62		// building POST-request:
 63		$request.="POST ".$URL_Info["path"]." HTTP/1.0\n";
 64		$request.="Host: ".$URL_Info["host"]."\n";
 65		$request.="Referer: $referrer\n";
 66		$request.="Content-type: text/plain\n";
 67		$request.="Content-length: ".strlen($data)."\n";
 68		$request.="Connection: close\n";
 69		$request.="\n";
 70		$request.= $data ."\n";
 71
 72	  $result = "";
 73		$fp = fsockopen($URL_Info["host"],$URL_Info["port"]);
 74		fputs($fp, $request);		
 75		// We don't capture the HTTP Header
 76		$bCapturing = false;
 77		while(!feof($fp)) {
 78				$curline = fgets($fp, 4096);
 79				if ($bCapturing) {
 80				  $result .= $curline;
 81				} elseif (strlen(trim($curline))==0) {
 82				  $bCapturing=true;
 83				} 
 84		}
 85		fclose($fp);
 86		
 87		/*
 88		echo "<b>HttpWrapper_PHP::post, Result = </b><br/><pre>";
 89		echo str_replace(array("<",">"),array("&lt;","&gt;"),$result);
 90		echo "</pre><hr>";
 91		*/
 92		
 93		return $result;
 94	}
 95}	
 96/**
 97 * Wrap HTTP Access.
 98 * Generates an appropriate wrapper for the system. If you would like to use a particular wrapper, just
 99 * modify this class method to return the appropriate wrapper for your system.
100 * 
101 * @access protected
102 */
103class HTTPWrapper {
104	function GetWrapper() {
105		if (extension_loaded("curl")) {
106			return new HttpWrapper_CURL();
107		} else {
108			return new HttpWrapper_PHP();
109		}
110	}
111}
112?>