PHP+socket编程发送HTTP请求

该文章介绍了一个使用PHP进行socket编程的示例,实现了通过HTTP请求模拟下载、注册和登录功能。代码定义了一个接口Proto,包含连接、GET、POST和关闭连接的方法。Http类实现了这个接口,支持设置请求行、头信息和主体信息,可用于批量发帖。

PHP+socket编程 发送HTTP请求
要求能 模拟下载,注册,登陆,批量发帖

// http请求类的接口
interface Proto {
    // 连接url
    function conn($url);

    //发送get查询
    function get();

    // 发送post查询
    function post();

    // 关闭连接
    function close();
}



class Http implements Proto {

    // carriage return line feed 回车换行
    const CRLF  = "\r\n";

    protected $errno = -1;
    protected $errstr = '';
    protected $response = '';

    protected $url = null;
    protected $version = 'HTTP/1.1';
    protected $fh = null;
    
    protected $line = array();
    protected $header = array();
    protected $body = array();

    
    public function __construct($url) {
        $this->conn($url);
        $this->setHeader('Host: ' . $this->url['host']);
    }

    // 此方法负责写请求行
    protected function setLine($method) {
        $this->line[0] = $method . ' ' . $this->url['path'] . ' '. $this->version;
    }

    // 此方法负责写头信息
    protected function setHeader($headerline) {
        $this->header[] = $headerline; 
    }

    // 此方法负责写主体信息
    protected function setBody($body) {
         $this->body[] = http_build_query($body);
    }


    // 连接url
    public function conn($url) {
        $this->url = parse_url($url);
        // 判断端口
        if(!isset($this->url['port'])) {
            $this->url['port'] = 80;
        }

        $this->fh = fsockopen($this->url['host'],$this->url['port'],$this->errno,$this->errstr,3);
    }

    //构造get请求的数据
    public function get() {
        $this->setLine('GET');
        $this->request();
        return $this->response;
    }

    // 构造post查询的数据
    public function post($body = array()) {   
        $this->setLine('POST');

        // 设计content-type
        $this->setHeader('Content-type: application/x-www-form-urlencoded');
        
        // 设计主体信息,比GET不一样的地方
        $this->setBody($body);


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

        $this->request();
    }

    // 真正请求
    public function request() {
        // 把请求行,头信息,实体信息 放在一个数组里,便于拼接
        $req = array_merge($this->line,$this->header,array(''),$this->body,array(''));
        //print_r($req);

        $req = implode(self::CRLF,$req); 
        //echo $req; exit;

        fwrite($this->fh,$req);
        
        while(!feof($this->fh)) {
            $this->response .= fread($this->fh,1024);
        }

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


    // 关闭连接
    public function close() {
        fclose($this->fh);
    }

    

}




/*
$url = 'http://news.163.com/13/0613/09/9187CJ4C00014JB6.html';

$http = new Http($url);
echo $http->get();
*/
/*
set_time_limit(0);

$url = 'http://liangyue.net.cn/0523/?';



for($i=1;$i<100;$i++) {
    $str = str_shuffle('abcdefghijklmnopqrst0776656');
    $tit = substr($str,0,5);
    $con = substr($str,6,8);

    $http = new Http($url);
    $http->post(array('tit'=>$tit,'con'=>$con,'submit'=>'留言'));

    echo $tit,'-----------',$con,'<br />';

    usleep(2000);
}

*/
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值