php代理访问

最近想写个软件玩玩,抓取网页上的内容

 

抓取网页内容的我放在一个文件中写成类了

 

以下是代码

 

<?php
class myhttp
{
    var $_host;
    var $_url;
    var $_port;
    var $_errno;
    var $_errstr;
    var $_header;
    var $_body;
   
    function __construct($url)
    {
        if($url) setUrl($url);
    }
   
    function setUrl($url)
    {
        if(preg_match("{http://([^/]+)[:(//d+)]?(.+)}",$url,$match))
        {
            $this->_host = $match[1];
            if(count($match) == 3)
            {
                $this->_port = 80;
                $this->_url = $match[2];
            }
            else
            {
                $this->_port = $match[2];
                $this->_url = $match[3];
            }
            $this->$_header = array();
            $this->$_body = "";
        }
        else
        {
            echo "请输入正确的网址。";
        }
    }
   
    function openPage()
    {
        $fstr = "";
        $fp = fsockopen($this->_host,$this->_port,&$_errno,&$_errstr,30);
        if($fp)
        {
            fputs($fp,"GET {$this->_url} HTTP/1.1/r/n".
            "Host:{$this->_host}/r/n".
            "User-Agent:Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.4) Gecko/20091016 Firefox/3.5.4/r/n".
            "Accept:image/png,image/*;q=0.8,*/*;q=0.5/r/n".
            "Accept-Language:zh-cn,zh;q=0.5/r/n".
            "Accept-Encoding:gzip,deflate/r/n".
            "Accept-Charset:GB2312,utf-8;q=0.7,*;q=0.7/r/n".
            "Keep-Alive:300/r/n".
            "Connection: Close/r/n/r/n");
            while(!feof($fp))
            {
                $fstr .= fgets($fp);
            }
            fclose($fp);
            $HtoB = strpos($fstr,"/r/n/r/n");
            $headerstr = substr($fstr,0,$HtoB);
            $ha= split("/r/n",$headerstr);
            foreach($ha as $v)
            {
                $h = split(":",$v);
                $this->_header[$h[0]] = $h[1];
            }
            $HtoB = strpos($fstr,"/r/n",$HtoB+4);
            $this->_body = substr($fstr,$HtoB+2);
            if(strpos("gzip",$this->_header["Content-Encoding"]) >= 0)
            {
                $this->_body = gzdecode($this->_body);
            }
        }
        return $this->_body;
    }
    function gzdecode($data)
    {
        $g=tempnam('/tmp','ff');
        @file_put_contents($g,$data);
        ob_start();
        readgzfile($g);
        $d=ob_get_clean();
        return $d;
    }
}
?>

 

调用代码如下

$httpobj = new myhttp();

$httpobj->setUrl($v);
$httpobj->openPage();  //获得指定页面的字符串

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值