// 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 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_)); }
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