Trieda na odosielanie HTTP požiadaviek.

<?
# http_post - PHP3 class for posting a 'form' from within a php3 script
#
# Alan van den Bosch (alan@sanguis.com.au)
#
# You are granted the right to use and/or redistribute this
# and you accept that no warranty of any kind is made or implied 
# 
# Methods:
# http_post() 
#    Returns true on success.
#        $a=new http_post;
#
#    Set the server of the URI you wish to post to. see also set_action()
#    ie.
#    or
#
# set_port(string PORT) 
#    Returns true on success.
#        $a->set_port("8080");
#
#    Set the filename of the URI you wish to    post to. see also set_action()
#    ie.
#
# set_action(string ACTION) 
#    Returns true on success.
#        $a->set_action("http://www.somehost.org:8080/incoming.php3");
# set_enctype(string ENCTYPE)
#    "application/x-www-form-urlencoded" or "multipart/form-data"
#    ie.
#
# set_element(string NAME, string VALUE)
#    Returns true on success.
#        $a->set_element("username","John Doe");
#
#    Set or update a number of name/value pairs to be posted
#    ie.
#                      "password" => "dead-ringer",
#
# set_timeout(integer TIMEOUT)
#    when posting. minimum value of 1 second.
#    ie.
#
#    Show the current internal state of an instance, for debugging.
#    ie.
#
# send(boolean DISPLAY)
#    can be echoed by setting DISPLAY to a true value. 
#    on failure.
#        $a->send(1);
 
 
class http_post
{
    function http_post(){
        $this->_method="post";
        $this->_server=$GLOBALS["HTTP_HOST"];
        $this->_file="\\";
        $this->_port="80";
        $this->_enctype="application/x-www-form-urlencoded";
        $this->_element=array();
        $this->_timeout=20;
    }
 
    function set_server($newServer=""){
        if(strlen($newServer)<1)$newServer=$HTTP_HOST;
        $this->_server=$newServer;
        return 1;
    }    
 
    function set_port($newPort="80"){
        $newPort=intval($newPort);
        if($newPort < 0 || $newPort > 65535)$newPort=80;
        $this->_port=$newPort;
        return 1;
    }    
 
    function set_file($newFile="\\"){
        $this->_file=$newFile;
        return 1;
    }    
 
    function set_action($newAction=""){
        $pat="^((http://){1}([^:/]{0,}){1}(:([0-9]{1,})){0,1}){0,1}(.*)";
 
        if(eregi($pat,$newAction,$sub)){
            if(strlen($sub[3])>0)$this->_server=$sub[3];
            if(strlen($sub[5])>0)$this->_port=$sub[5];
            $this->_file=$sub[6];
            return 1;
        }
        return 0;
    }
 
    function set_enctype($newEnctype="application/x-www-form-urlencoded"){
        if($newEnctype != "application/x-www-form-urlencoded" &&
            $newEnctype != "multipart/form-data"){
            $newEnctype="application/x-www-form-urlencoded";
        }
        $this->_enctype=$newEnctype;
        return 1;
    }    
 
    function set_element($key="",$val=""){
        if(is_array($key)){
            $len=sizeof($key);
            reset($key);
            for($i=0;$i<$len;$i++){
                $cur=each($key);
                $k=$cur["key"];
                $v=$cur["value"];
                $this->_element[$k]=$v;
            }
        }
        else{
            if(strlen($key)>0)$this->_element[$key]=$val;
        }
        return 1;
    }
 
    function set_timeout($newTimeout=20){
        $newTimeout=intval($newTimeout);
        if($newTimeout<1)$newTimeout=1;
        $this->_timeout=$newTimeout;
        return 1;
    }    
 
    function show_post(){
        $str="";
        $str.="Action:".$this->_action."<br>";
        $str.="Server:".$this->_server."<br>";
        $str.="Port:".$this->_port."<br>";
        $str.="File:".$this->_file."<br>";
        $str.="Enctype:".$this->_enctype."<br>";
 
        echo $str;
 
        $len=sizeof($this->_element);
        reset($this->_element);
        for($i=0;$i<$len;$i++){
            $cur=each($this->_element);
            $key=$cur["key"];
            $val=$cur["value"];
            echo"Field:$key = $val<br>\n";
        }
        return 1;
    }
 
    function send($display=0){
        // open socket to server
        $errno=$errstr=$retstr="";
        $sk = fsockopen($this->_server,
                $this->_port,
                &$errno,
                &$errstr,
                $this->_timeout
                );
        if(!$sk){
            return 0;
        }
        else{
            $boundary="----".md5(uniqid(rand()))."----";
            $message=$this->_get_message($boundary);
            $str="";
            $str.=strtoupper($this->_method)." ";
            $str.=$this->_file." HTTP/1.0 \r\n";
            $str.="Referer: \r\n";
            $str.="User-Agent: php-HTTP_POST/1.0 \r\n";
            $str.="Host: ".$this->_server."\r\n";
 
            $str.="Content-type: ".$this->_enctype;
            if($this->_enctype=="multipart/form-data"){
                $str.="; boundary=".$boundary;
            }
            $str.=" \r\n";
 
            $str.="Content-length: ".strlen($message)."\r\n\r\n";
            $str.=$message;
 
            fputs($sk,$str);
 
            while(!feof($sk)){
                $resp=fgets($sk,80);
                $retstr.=$resp;
                if($display)echo $resp;
            }
 
            fclose($sk);
            return $retstr;
        }
    }        
 
    function _get_message($boundary=""){
        $retstr="";
 
        $len=sizeof($this->_element);
        reset($this->_element);
 
        $switch=($this->_enctype=="multipart/form-data")?0:1;
 
        for($i=0;$i<$len;$i++){
            $cur=each($this->_element);
            $key=$cur["key"];
            $val=$cur["value"];
 
            if($switch){
                if(strlen($retstr)!=0)$retstr.="&";
                $retstr.=rawurlencode($key)."=";
                $retstr.=rawurlencode($val);    
            }
            else{
                $retstr.=$boundary."\r\n";
                $retstr.="Content-Disposition: form-data; ";
                $retstr.="name=\"$key\"\r\n\r\n$val\r\n\r\n";
            }
        }
        if(!$switch)$retstr.=$boundary."\r\n";
        return $retstr;
    }
}
 
?>