myhttp类 php使用fsockopen实现稳定的HTTP连接(转)

网上找到一个用fsockopen函数实现http请求的类,感觉不错。

<?php
    /*
    CatSeven myHttp Vesion 0.1
 
    ======CopyRight======
    Home:http://www.myw3.cn/myDevise/myHttp/
    Design:Miao Qiyuan[miaoqiyuan.cn]
    Downloads:http://downloads.myw3.cn/file=myDevise/myHttp/0.1
    */
 
    class myHttp{
        public $Method,$URI,$SendDate;
        public $HttpServerPort,$HttpServer,$HttpServerIP;
        public $Err,$ErrStr;
        public $timeout;
        public $responseText;
 
        public function __construct($uri='/',$method='get',$query='',$server='localhost',$port='80',$serverip='',$timeout=30){
            $this->URI=$uri;
            $this->Method=$method;
            $this->SendDate=$query;
            $this->HttpServer=$server;
            $this->HttpServerPort=$port;
            $this->HttpServerIP=$serverip;
            if(is_numeric($timeout))$this->timeout=$timeout;
        }
 
        public function send(){
            $this->Method=strtoupper($this->Method);
            if($this->HttpServerIP=="")$this->HttpServerIP = $this->HttpServer;
            if($this->Method=="GET" && strstr($this->URI,"?")==0)$this->URI=$this->URI."?".$this->SendDate;
            $sock = fsockopen($this->HttpServerIP,$this->HttpServerPort,$errno,$errstr,$this->timeout);
            if(!$sock){
                $this->ErrStr=$errstr;
                $this->Err=$errno;
                die("无法打开".$this->HttpServerIP.":".$this->HttpServerPort);
            }
            fwrite($sock, $this->Method." ".$this->URI." HTTP/1.0\r\n");
            fwrite($sock, "Host: ".$this->HttpServer."\r\n");
            if($this->Method=="POST"){
                fwrite($sock, "Content-type: application/x-www-form-urlencoded\r\n");
                fwrite($sock, "Content-length: ".strlen($this->SendDate) . "\r\n");
                fwrite($sock, "Accept: */*\r\n");
                fwrite($sock, "\r\n");
                fwrite($sock, $this->SendDate."\r\n");
                fwrite($sock, "\r\n");
                fwrite($sock, "Referer: http://www.myw3.cn/myDevise/myHttp/");
            }
            fwrite($sock, "Connection: Close\r\n\r\n");
            $headers = "";
            while ($str = trim(fgets($sock,4096)))
                $headers .= "$str\n";
 
            $body = "";
            while (!feof($sock))
                $body .= fgets($sock, 4096);
            fclose($sock);
            $this->responseText=$body;
        }
    }
?>
 
输入参数的方法有两种,在创建类的时候同时输入参数和先创建类,慢慢输入参数。相关例子分别为test,test2,该类同时支持get,post的方法。
<?php
    include("myHttp.class.php");
    $test = new myHttp();
    $test -> URI = "/index.php";
    $test -> HttpServer = "www.miaoqiyuan.cn";
    $test -> Method = "post";
    $test -> SendDate ="s=myhttp";
    $test -> send();
    echo $test -> responseText;
 
    $test2 = new myHttp('/index.php','get','s=myhttp','www.miaoqiyuan.cn','80','',30);
    $test2 -> send();
    echo $test2 -> responseText;
?>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值