//兼容php4php5
//程序作者张建52linux.com(我爱Linux)
//联系方法733905@qq.comQQ733905
//简单调用方法
/*
include("ugs.php");//你可以下载本ugs.phps然后重命名为ugs.php
$ugs=newugs();
$url="http://domainname.com/path_to_your_target?param";
$ugs->seturl($url);
$ugs->gather();
//............这里可以调用本类里的其它方法,对$ugs->value_做调整,
以满足您的要求
$content=$ugs->getcontent();
print($content);
?>
*/
classugs
{
var$value_;//'目标内容
var$src_;//'目标URL地址
functionseturl($url)
{
$this->src_=$url;
}
functiongetcontent()
{
return$this->value_;
}
functiongetfile($url)
//获取目标
{
$url_parsed=parse_url($url);
$host=$url_parsed["host"];
$port=$url_parsed["port"];
if($port==0)$port=80;
$path=$url_parsed["path"];
if(empty($path))
$path="/";
if($url_parsed["query"]!="")
$path.="?".$url_parsed["query"];
$out="GET$pathHTTP/1.0rnHost:$hostrnrn";
$fp=fsockopen($host,$port,$errno,$errstr,30);
fwrite($fp,$out);
$body=false;
while(!feof($fp))
{
$s=fgets($fp,1024);
if($body)$in.=$s;
if($s=="rn")
$body=true;
}
fclose($fp);
return$in;
}
functiongetfile_curl($url)
{
$curl="/usr/local/bin/curl";//pathtoyourcurl
$curl_options="-s--connect-timeout10--max-time10";
//curl用法请参考curl--help或者mancurl
//curl参数非常之丰富,可以模拟各种浏览器(agent)可以设置referer
$cmd="$curl$curl_options$url";
@exec($cmd,$o,$r);
if($r!=0)
{
return"超时";
}
else
{
$o=join("",$o);
return$o;
}
}
functiongather_curl($curl)
{
$http=$this->getfile_curl($this->src_);
return$this->value_=$http;
}
functiongather_array($url)
{
returnfile($url);
}
functiongather()
//开始收集
{
$http=$this->getfile($this->src_);
return$this->value_=$http;
}
functiongather_local($toline=true)
//处理本地文件
{
if($toline)
{
$http=file($this->src_);
return$this->value_=$this->BytesToBstr($http);
}
else
{
$http=file($this->src_);
return$this->value_=$http;
}
}
functionnoReturn()
//删除回车换行
{
$this->value_=str_replace("n","",$this->value_);
$this->value_=str_replace("r","",$this->value_);
}
functionchange($oldStr,$str)
//'对收集到的内容中的个别字符串用新值更换/方法
//'参数分别是旧字符串,新字符串
{
$this->value_=str_replace($oldStr,$str,$this->value_);
}
functioncut($start,$end,$no='1',$comprise='')
//'按指定首尾字符串对收集的内容进行裁减(不包括首尾字符串)方法
//$no必须是1,23...不允许是0
//$comprise可以选择start或者end或者all或者什么都不填
{
$string=explode($start,$this->value_);
//print_r($string);
$string=explode($end,$string[$no]);
//print_r($string);
switch($comprise){
case'start':
$string=$start.$string[0];
break;
case'end':
$string=$string[0].$end;
break;
case'all':
$string=$start.$string[0].$end;
break;
default:
$string=$string[0];
}
return$this->value_=$string;
}
functionfilt($head,$bot,$str,$no='1',$comprise='')
//'按指定首尾字符串对收集的内容用新值进行替换(不包括首尾字符串)方法
//'参数分别是首字符串,尾字符串,新值,新值位空则为过滤
{
$tmp_v=$this->value_;
$tmp=$this->cut($head,$bot,$no,$comprise);
return$this->value_=str_replace($tmp,$str,$tmp_v);
}
functionlocal()
{
//'将收集的内容中的绝对URL地址改为本地相对地址
//还没实现
}
functionreplaceByReg($patrn,$str)
//'对收集的内容中的符合正则表达式的字符串用新值进行替换/方法
//'参数是你自定义的正则表达式,新值
{
return$this->value_=join("",preg_replace($patrn,$str,$this->value_));
}
functiondebug()
//调试显示
{
$tempstr="
echo$tempstr;
}
}
?>