最近想写个软件玩玩,抓取网页上的内容
抓取网页内容的我放在一个文件中写成类了
以下是代码
<?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(); //获得指定页面的字符串