http.class.php

1.声明个接口

interface Proto
{
    // 连接url
    function conn($url);
    // get查询
    function get();
    // post查询
    function post();
    // 关闭连接
    function close();
}

2.实现类

构造请求行、请求头信息、请求主体信息

class Http implements Proto
{
    //请求行、请求头信息、请求主体信息
    protected $url = null;
    protected $fh = null;
    protected $line = array();
    protected $version = 'HTTP/1.1'; 
    protected $header = array();
    protected $body = array();
    protected $response = '';
    protected $errno = -1;
    protected $errstr = '';

    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->url['query'] . ' ' . $this->version;
    }

    // 请求头信息
    public function setHeader($headerline)
    {
        $this->header[] = $headerline;
    }

    // 请求主体信息
    protected function setBody($body)
    {
        // $this->body = array_merge($this->body,$body);
        $this->body[] = http_build_query($body);
    }

    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);
    }
    
    public function get()
    {
        $this->setLine('GET');
        $this->request();
        return $this->response;
    }

    public function post($body = array())
    {
        $this->setLine('POST');

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

        $this->setHeader('Content-length: ' . strlen($this->body[0]));
        $this->request();
        return $this->response;
    }

    // request里面合并请求数组
    public function request()
    {
        $req = array_merge($this->line, $this->header, array(''), $this->body,array(''));
        $req =  implode(PHP_EOL, $req);
        // echo $req;die;
        fwrite($this->fh, $req);
        while(!feof($this->fh))
        {
            $this->response .= fread($this->fh,1024);
        }
        $this->close();
        // return $this->response;
        // var_dump($this->response);
    }

    function close()
    {
        fclose($this->fh);
    }

    
}

// $url = 'http://news.163.com/15/1219/10/BB6K60P000014PRF.html';
// $http = new Http($url);
// echo $http->get();
// $str = str_shuffle('abcddedghijklmnopqrstuvwxyz0123456789');
// $message = substr($str, 0,10);
// $posttime = time();
// $msg = array('message'=>$message, 'posttime'=>$posttime, 'formhash'=>'b49016a2');
// $url = 'http://home.verycd.com/cp.php?ac=pm';
// $http = new Http($url);

// echo $http->post($msg);

?>

3.POST实例

<?php

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

$http = new Http('http://home.verycd.com/cp.php?ac=pm&op=send&touid=0&pmid=0');
$http->setHeader('Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8');
$http->setHeader('Accept-Encoding: gzip, deflate');
$http->setHeader('Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3');
$http->setHeader('Connection: keep-alive');
$http->setHeader('Cookie: Hm_lvt_c7849bb40e146a37d411700cb7696e46=1450578346,1450591704; member_id=20803098; member_name=sherlockowen_qq; mgroupId=93; pass_hash=0b4a81656fd19e44165acee8ebbb1b4d; rememberme=true; uchome_auth=8333FURQDypWf%2BTDBYM52Xg9nMWGA6XJpkPgAHBt9u1P1ZTDEHBeSAlp524uHrRuSwm4l7R2n%2BuxECzK0fMAqD%2BchBs4NQ; uchome_loginuser=sherlockowen_qq; CNZZDATA1479=cnzz_eid%3D1888860155-1450577385-http%253A%252F%252Fwww.verycd.com%252F%26ntime%3D1450587438; __utma=248211998.1163897337.1450578504.1450587423.1450591718.3; __utmz=248211998.1450591718.3.2.utmcsr=verycd.com|utmccn=(referral)|utmcmd=referral|utmcct=/search/entries/%E7%87%95%E5%8D%81%E5%85%AB; sid=f1be7b600541e84b1c8df9283a1f15ac32b69693; Hm_lpvt_c7849bb40e146a37d411700cb7696e46=1450591704; uchome_sendmail=1; __utmb=248211998.2.10.1450591718; __utmc=248211998; __utmt=1');
$http->setHeader('Host: home.verycd.com');
$http->setHeader('Referer: http://home.verycd.com/cp.php?ac=pm');
$http->setHeader('User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0');
// $http->setHeader();

$msg = array(
    'formhash'=>'5e5cb85a',
    'message'=>'dindidngdhh',
    'pmsubmit'=>'true',
    'pmsubmit_btn'=>'发送',
    'refer'=>'http://home.verycd.com/space.php?do=pm&filter=newpm',
    'username'=>'罗本爱范佩西_sina',
    );
file_put_contents('./re.html', $http->post($msg));
// echo $http->post($msg);
?>

 

posted on 2016-01-15 00:18  阿卡贝拉  阅读(435)  评论(0)    收藏  举报