Lover雪儿
想念时,就看看天空,无论距离有多远,我们总在同一片天空下!

PHP实现HTTP的POST与GET 类

2015-01-25

http.class.php代码:

<?php

/*PHP + socket 编程,发送HTTP请求

* 要求能模拟下载,注册,登陆,批量发站

* */

//http 请求类的接口

interfaceProto{

    //连接url

    publicfunction conn($url);

 

    //发送get查询

    publicfunction get();

 

    //发送post查询

    publicfunction post();

 

    //关闭连接

    publicfunction close();

}

 

 

classHttpimplementsProto{

    protected$url = array();

    protected$version = 'HTTP/1.1';    //请求协议

    protected$fh =null;        //连接信息

    constCRLF = "\r\n";        // carriage return line feed;

    protected$errno = -1;        //错误号

    protected$errstr = '';

 

    protected$response = '';

 

    protected$line = array();     //请求行

    protected$header = array();    //头信息

    protected$body = array();    //主体信息

 

    publicfunction __construct($url){

        $this->conn($url);

        $this->setHeader("Host:".$this->url['host']);

    }

    

    //此方法负责写请求行

    protectedfunction setLine($method){

        $this->line[0] =$method.' '.$this->url['path'].'? '.$this->url['query'].' '.$this->version;    

    }

 

    //此方法负责写头信息

    publicfunction setHeader($headerline){

        $this->header[] =$headerline;

    }

 

    //此方法负责写主体信息

    protectedfunction setBody($body){    

        $this->body[] =http_build_query($body);//将数组构建成HTTP请求字符串

        //print_r($this->body);

    }

    

    //连接url

    publicfunction conn($url){

        $this->url =parse_url($url);

        if(!isset($this->url['port'])){

            $this->url['port'] = 80;

        }

        if(!isset($this->url['query'])){

            $this->url['query'] = '';

        }

        //print_r($this->url);

        $this->fh =fsockopen($this->url['host'],$this->url['port'],$this->errno,$this->errstr,3);    //连接主机,错误号,错误字符串,超时时间:3s

    }

    //构造get查询的数据

    publicfunction get(){

        $this->setLine('GET');

        $this->request();

        return$this->response;

    }

 

    //构造post查询的数据

    publicfunction post($body = array()){

        $this->setLine('POST');    //设置请求行

        $this->setHeader("Content-type:application/x-www-form-urlencoded");//设置content-type

        $this->setBody($body);//设计主体信息,比GET不一样

        $this->setHeader('Content-length:'.strlen($this->body[0]));//计算content-length

        $this->request();

        return$this->response;

    }

 

    // 真正请求

    publicfunction request(){

        //把头信息,实体信息,拼接起来

        //$req = array_merge($this->line,$this->header,array(''), array(http_build_query($body)),array(''));

        $req =array_merge($this->line,$this->header,array(''),$this->body,array(''));

        //print_r($req);

        $req =implode(self::CRLF,$req);

        echo "请求行信息:<br/>",$req;        //拼接头信息

        fwrite($this->fh,$req);

        //print_r($this->fh);

        $this->response .= "<br/><br/>返回行信息:<br/>";

        while(!feof($this->fh)){

            $this->response .=fread($this->fh,1024);

        }

 

        $this->close();        //关闭连接

    }

 

    //关闭连接

    publicfunction close(){

        fclose($this->fh);

    }

}

/*

require('./http.class.php');

 

$msg = array(

        'formhash'=>'4f23e777',

        'message'=>'world',

        'pmsubmit'=>'true',

        'pmsubmit_btn'=>'发送',

        'refer'=>'http://localhost/foruser/HTTP/testhttp.php',

        'username'=>'http接收',

        'user'=>'Lover雪'

    );

 

 

$http = new Http("http://localhost/foruser/HTTP/testhttp.php");

$http->setHeader("cookie: user=Lover雪"); //使用COOKIE

 

echo "<h1>POST请求</h1>";

echo $http->post($msg);

 

$http = new Http("http://localhost/foruser/HTTP/testhttp.php");

$http->setHeader("cookie: user=Lover雪");

echo "<h1>GET请求</h1>";

echo $http->get();

*/

?>

 

testhttp.php代码:

<?php

if(isset($_COOKIE['user']))

    echo "<h2>I Know You Are ".$_COOKIE['user']."</h2>";

 

echo "<br/><br/>http:接收信息<br/>";

if(isset($_POST))

    print_r($_POST);

?>

 

实验结果图:

 

 

posted on 2015-02-05 10:47  Lover雪儿  阅读(1124)  评论(0编辑  收藏  举报