Home > Backend Development > PHP Tutorial > A data collection class_PHP tutorial

A data collection class_PHP tutorial

WBOY
Release: 2016-07-21 15:57:57
Original
825 people have browsed it

Copy code The code is as follows:

// Compatible with php4 php5
// Program author Zhang Jian 52linux.com (I love Linux)
// Contact method 733905@qq.com QQ 733905
// Simple calling method
/*
include ("ugs.php"); // You can download this ugs.phps and rename it to ugs.php
$ugs = new ugs();
$url = "http://domainname.com/path_to_your_target?param";
$ugs->seturl($url);
$ugs->gather( );
//......Here you can call other methods in this class to adjust $ugs->value_,
to meet your requirements
$content=$ugs->getcontent();
print($content);
?>
*/
class ugs
{
var $value_ ; // 'Target content
var $src_ ; //'Target URL address

function seturl($url)
{
$this->src_=$url;
}
function getcontent()
{
return $this->value_;
}
function getfile($url)
// Get target
{
$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.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;
        }
                                                                                                                    local/bin/curl " ; // path to your curl
$curl_options=" -s --connect-timeout 10 --max-time 10 ";
// For curl usage, please refer to curl --help or man curl
// curl has very rich parameters, it can simulate various browsers (agents) and can set referer
$cmd="$curl $curl_options $url ";
@exec($cmd,$ o,$r);
if($r!=0)
{
return "timeout";
}
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='')
//'Cut the collected content according to the specified first and last strings Cutting (excluding first and last string) method
// $no must be 1,2 3... 0 is not allowed
//$comprise can choose start or end or all or fill in nothing
{
$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; $end;
break;
default:
$string=$string[0];
}
return $this->value_=$string;
       }


function filt($head,$bot,$str,$no='1',$comprise='')
//'Use new values ​​for the collected content according to the specified first and last strings Replacement (excluding the first and last strings) method
// 'The parameters are the first string, the last string, and the new value. If the new value is empty, it will be filtered
{
$tmp_v=$this-> ;value_;
$tmp=$this->cut($head,$bot,$no,$comprise);
return $this->value_=str_replace($tmp,$str,$tmp_v );
}



function local()
{
/ Not yet implemented
}



function replaceByReg($patrn,$str)
//' Use new strings that match regular expressions in the collected content Value replacement/method
//'The parameter is your custom regular expression, the new value
{
return $this->value_=join("",preg_replace($patrn,$str ,$this->value_));
}



function debug()
//Debug display
{
$tempstr="< SCRIPT>function runEx(){var winEx2 = window.open("", "winEx2", "width=500,height=300,status=yes,menubar=no,scrollbars=yes,resizable=yes"); winEx2. document.open("text/html", "replace"); winEx2.document.write(unescape(event.srcElement.parentElement.children[0].value)); winEx2.document.close(); }function saveFile( ){var win=window.open('','','top=10000,left=10000');win.document.write(document.all.asdf.innerText);win.document.execCommand('SaveAs' ,'','javascript.htm');win.close();}


echo $tempstr;
}


}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/317743.htmlTechArticleCopy the code as follows: ? //Compatible with php4php5 //Program author Zhang Jian52linux.com (I love Linux) //Contact method 733905@qq.comQQ733905 //Simple calling method/* ? include("ugs.php");//You can...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template