PHP Thief Program Instance Code _php tutorial

Source: Internet
Author: User
Thief program is actually the use of a specific PHP function to collect the content of other people's website, and then through regular analysis of the content we want to save to their local database, the following I introduce the PHP Thief program implementation method, there is a need for friends to refer.

The file_get_contents function is key in the process of collecting data below, so let's take a look at the file_get_contents function syntax

String file_get_contents (String $filename [, bool $use _include_path = False [, resource $context [, int $offset =-1 [, int $maxlen]])
As with file (), only file_get_contents () reads a file into a string. Reads the contents of length MaxLen at the position specified by the parameter offset. If it fails, file_get_contents () returns FALSE.

The file_get_contents () function is the preferred method for reading the contents of a file into a string. If operating system support also uses memory-mapping techniques to enhance performance.

Cases

The code is as follows Copy Code

$homepage = file_get_contents (' http://www.hzhuti.com/');
Echo $homepage;
?>

So $homepage is our collection of the content of the net to save, well, so much to say we begin.

Cases

The code is as follows Copy Code

function Fetch_urlpage_contents ($url) {
$c =file_get_contents ($url);
return $c;
}
Get matching content
function fetch_match_contents ($begin, $end, $c)
{
$begin =change_match_string ($begin);
$end =change_match_string ($end);
$p = "{$begin} (. *) {$end}";
if (eregi ($p, $c, $rs))
{
return $rs [1];}
else {return "";}
}//escaping the regular expression string
function change_match_string ($STR) {
Note that the following is just a simple escape
$old =array ("/", "$");
$new =array ("/", "$");
$str =str_replace ($old, $new, $STR);
return $str;
}

Collect Web pages
function pick ($url, $ft, $th)
{
$c =fetch_urlpage_contents ($url);
foreach ($ft as $key = $value)
{
$rs [$key]=fetch_match_contents ($value ["Begin"], $value ["End"], $c);
if (Is_array ($th [$key]))
{foreach ($th [$key] as $old = + $new)
{
$rs [$key]=str_replace ($old, $new, $rs [$key]);
}
}
}
return $rs;
}

$url = "http://www.bkjia.com"; The address to collect
$ft ["title"] ["Begin"]= ""; Intercept start point <br/> $ft ["title"] ["End"]= ""; End point of intercept
$th ["title"] ["Zhongshan"]= "Guangdong"; The replacement of the interception section

$ft ["Body"] ["Begin"]= ""; Start point of interception
$ft ["Body"] ["End"]= ""; End point of intercept
$th ["Body"] ["Zhongshan"]= "Guangdong"; The replacement of the interception section

$rs =pick ($url, $ft, $th); Start collecting

echo $rs ["title"];
echo $rs ["body"]; Output
?>

The following code is modified from the previous side to extract all hyperlinks, mailboxes, or other specific content of the Web page.

The code is as follows Copy Code

function Fetch_urlpage_contents ($url) {
$c =file_get_contents ($url);
return $c;
}
Get matching content
function fetch_match_contents ($begin, $end, $c)
{
$begin =change_match_string ($begin);
$end =change_match_string ($end);
$p = "#{$begin} (. *) {$end} #iU";//i means ignore case, u suppress greedy match
if (Preg_match_all ($p, $c, $rs))
{
return $rs;}
else {return "";}
}//escaping the regular expression string
function change_match_string ($STR) {
Note that the following is just a simple escape
$old =array ("/", "$", '? ');
$new =array ("/", "$", '? ');
$str =str_replace ($old, $new, $STR);
return $str;
}

Collect Web pages
function pick ($url, $ft, $th)
{
$c =fetch_urlpage_contents ($url);
foreach ($ft as $key = $value)
{
$rs [$key]=fetch_match_contents ($value ["Begin"], $value ["End"], $c);
if (Is_array ($th [$key]))
{foreach ($th [$key] as $old = + $new)
{
$rs [$key]=str_replace ($old, $new, $rs [$key]);
}
}
}
return $rs;
}

$url = "http://www.bkjia.com"; The address to collect
$ft ["A"] ["Begin"]= '
$ft ["A"] ["End"]= ' > '; End point of intercept

$rs =pick ($url, $ft, $th); Start collecting

Print_r ($rs ["a"]);

?>

small hint file_get_contents is very easy to be anti-collection, we can use curl to imitate the user to visit the site, this is more advanced than the above to a lot of Oh, file_get_contents () efficiency slightly lower, common failure situation, Curl () is highly efficient and supports multi-threading, but needs to turn on the next Curl extension. Here are the steps the Curl extension opens:

1, the PHP folder under the three files Php_curl.dll,libeay32.dll,ssleay32.dll copied to System32;

2, the php.ini (c:windows directory) in the; Extension=php_curl.dll in the semicolon removed;

3. Restart Apache or IIS.

Simple fetching of page functions with forged Referer and user_agent functions

The code is as follows Copy Code

function Getsources ($URL, $User _agent= ", $Referer _url=")//Fetch a specified page
{
$URL the page address to crawl
$User _agent need to return user_agent information such as "Baiduspider" or "Googlebot"
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $URL);
curl_setopt ($ch, curlopt_useragent, $User _agent);
curl_setopt ($ch, Curlopt_referer, $Referer _url);
curl_setopt ($ch, curlopt_followlocation,1);
curl_setopt ($ch, Curlopt_returntransfer, 1);
$MySources = curl_exec ($ch);
Curl_close ($ch);
return $MySources;
}
$URL = "http://www.bkjia.com"; There's no need to get content.
$User _agent = "baiduspider+ (+http://www.baidu.com/search/spider.htm)";
$Referer _url = ' http://www.jb51.net/';
Echo getsources ($URL, $User _agent, $Referer _url);
?>


http://www.bkjia.com/PHPjc/444620.html www.bkjia.com true http://www.bkjia.com/PHPjc/444620.html techarticle thief program is actually using a specific function in PHP to collect the content of other people's website, and then through regular analysis of the content we want to save to their local database, the following ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.