Location: PHPKode > scripts > XMethod > xmethod/class.XMethod.php
<?php
/*
 * 	This PHP Class translates variables transferred using the GET-method
 *  to POST-method and vice versa. Basically there is absolutely no use for this
 * 	(that I can think of) except that it gave me a chance to play around with 
 *  sending post requests :)
 */
Class XMethod
{
    function autoDetect() {
        if ($_SERVER['REQUEST_METHOD'] == "GET") {
        	$url = urldecode($_GET['translate_url']);
        	$_GET['ORIGINAL_METHOD'] = "GET";
        	$result = $this->sendpost($_GET, $url);
        
        	if (isset($result["errno"])) { 
            	$errno = $result["errno"]; 
        	    $errstr = $result["errstr"]; 
            	echo "<B>Error $errno</B> $errstr"; 
        	    exit; 
          	} else { 
        		$flag=0;
        	    for($i=0;$i< (count($result)-3); $i++) 
        		{
        			// stripping server blahblah here... :)
        			if ($flag==0){
        				if ($result[$i]=="\r\n") $flag=1; $i++;
        			} else {
        			echo $result[$i]; }
        		}
        	} 
        
        } elseif ($_SERVER['REQUEST_METHOD'] == "POST") {
        	$url = $_POST['translate_url'];
        	$_POST['ORIGINAL_METHOD'] = "POST";
        
        	$this->sendget($_POST, $url);
        
        } else { die('Nothing to do.'); }
    }
    
    /**
     * XMethod::posttoget()
     * 
     * @param $datastream -> Any associative array containing the key/value pairs to send
     * @param $url -> URL to send the data to.
     * @return 
     */
    function sendget($datastream, $url)
    {
        foreach ($datastream as $key=>$val) { 
    		if (!empty($requestmain)) { $requestmain.= "&"; }
          	$requestmain.= $key."=".urlencode($val); 
        } 
    	
    	header("Location: $url?$requestmain");
    }
    
    function sendpost($datastream, $url) 
    // based on an article by John Coggeshall (www.zend.com)
    { 
    	$url = preg_replace("@^http://@i", "", $url); 
    	$host = substr($url, 0, strpos($url, "/")); 
    	$uri = strstr($url, "/"); 
    
    	$requestmain = ""; 
        foreach($datastream as $key=>$val) { 
    		if (!empty($requestmain)) $requestmain.= "&"; 
          	$requestmain.= $key."=".urlencode($val); 
        } 
    	
    	$contentlength = strlen($requestmain); 
        $requestheader =  "POST $uri HTTP/1.1\r\n". 
                      	  "Host: $host\n". "User-Agent: XMethod\r\n". 
    			      	  "Content-Type: application/x-www-form-urlencoded\r\n". 
         			  	  "Content-Length: $contentlength\r\n\r\n". 
         			  	  "$requestmain\r\n"; 
    
    	$socket = fsockopen($host, 80, $errno, $errstr); 
    
    	if (!$socket) { 
       		$result["errno"] = $errno; 
       		$result["errstr"] = $errstr; 
       		return $result; 
    	} 
    
    	fputs($socket, $requestheader); 
    
    	while (!feof($socket)) { 
       		$result[] = fgets($socket, 4096); 
    	} 
    
    	fclose($socket); 
    
    	return $result; 
    } 
}
?>
Return current item: XMethod