複製代碼 代碼如下:
// 相容 php4 php5
// 程式作者 張建 52linux.com(我愛Linux)
// 聯絡方法 733905@qq.com QQ 733905
// 簡單調用方法
/*
include ("ugs.php"); // 你可以下載本ugs.phps 然後重新命名為ugs.php
$ugs = new ugs();
$url = "http://domainname.com/path_to_your_target?param";
$ugs->seturl($url);
$ugs->gather();
//............這裡可以調用本類裡的其它方法,對$ugs->value_ 做調整,
以滿足您的要求
$c
print($content);
?>
*/
class ugs
{
var $value_ ; //'目標內容
var $src_ ; //'目標URL地址
function seturl($url)
{
$this->src_=$url;
}
function getcontent()
{
return $this->value_;
}
function getfile($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 $path HTTP/1.0\r\nHost: $host\r\n\r\n";
$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 == "\r\n" )
$body = true;
}
fclose($fp);
return $in;
}
function getfile_curl($url)
{
$curl="/usr/local/bin/curl " ; // path to your curl
$curl_opti
// curl 用法請參考 curl --help 或者 man curl
// curl 參數非常之豐富,可以類比各種瀏覽器(agent) 可以設定referer
$cmd="$curl $curl_options $url ";
@exec($cmd,$o,$r);
if($r!=0)
{
return "逾時";
}
else
{
$o=join("",$o);
return $o;
}
}
function gather_curl($curl)
{
$http=$this->getfile_curl($this->src_);
return $this->value_=$http;
}
function gather_array($url)
{
return file($url);
}
function gather()
// 開始收集
{
$http=$this->getfile($this->src_);
return $this->value_=$http;
}
function gather_local($toline=true)
// 處理本地檔案
{
if($toline)
{
$http=file($this->src_);
return $this->value_=$this->BytesToBstr($http);
}
else
{
$http=file($this->src_);
return $this->value_=$http;
}
}
function noReturn()
// 刪除斷行符號換行
{
$this->value_=str_replace("\n","",$this->value_);
$this->value_=str_replace("\r","",$this->value_);
}
function change($oldStr,$str)
//'對收集到的內容中的個別字串用新值更換/方法
//'參數分別是舊字串,新字串
{
$this->value_=str_replace($oldStr,$str,$this->value_ );
}
function cut($start,$end,$no='1',$comprise='')
//'按指定首尾字串對收集的內容進行裁減(不包括首尾字串)方法
// $no 必須是 1,2 3 ... 不允許是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;
}
function filt($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);
}
function local()
{
//'將收集的內容中的絕對URL地址改為本地相對位址
// 還沒實現
}
function replaceByReg($patrn,$str)
//'對收集的內容中的符合Regex的字串用新值進行替換/方法
//'參數是你自訂的Regex,新值
{
return $this->value_=join("",preg_replace($patrn,$str,$this->value_));
}
function debug()
//調試顯示
{
$tempstr="
".$this->value_."
";
echo $tempstr;
}
}
?>
以上就介紹了無線資料擷取 一個資料擷取類,包括了無線資料擷取方面的內容,希望對PHP教程有興趣的朋友有所協助。