This paper describes the design and implementation of the PHP thief program. Share to everyone for your reference, as follows:
In fact, I have always wanted to do a content image of the site, the previous idea is to do a CMS, and then upload some of their own pictures.
At first, there was no incentive to do so. Then gave up, and later studied a curl. Anyway, it's better to make this idea.
Using PHP to steal pictures is like: wear socks and sandals. It's no problem, but it's really painful to watch.
Let me talk about my design of the PHP Thief program, PHP does not support multi-threading, so it can only be done in order to do
Get HTML page to destination site + Parse HTML page get connection to picture Store + read in binary mode and save locally + rename = = Process OK
You can now run the program in two ways:
The first kind: Run the program with the browser (most will be stuck, set the timeout and memory size OK, you are more difficult to wait)
Another: Start php with the command line (there is no problem with PHP timeouts)
/***html Parsing class *author:summer*date:2014-08-22**/class analytical{public function __construct () {require_once (' Class/S ImpleHtmlDom.class.php '); $this->_getdir (); } Private Function _getdir () {$dir = ": /TMP/HTML/RESULTS/1 "; $imgBIG = ". /tmp/img/jpg/big "; $it = new Directoryiterator ($dir. " /"); foreach ($it as $file) {//Use the Isdot () method to filter out "." Separately. and ".." Directory if (! $it->isdot ()) {$dirs = $dir. " /". $file; $tmp = Explode (".", $file); $html = file_get_html ($dirs); $ULARR = $html->find (' img '); foreach ($ulArr as $key = + $value) {if ($value->class = = "U") {$url = <a>http:/ /</a>www.jb51.net. $value->src; $infomation = file_get_contents ($url); $result = $this->savehtml ($infomation, $imgBIG, $tmp [' 0 ']. ". JPG "); if ($result) {echo $file. " Ok\n "; }}}}}} Private Function savehtml ($infomation, $filedir, $fIlename) {if (! $this->mkdirs ($filedir)) {return 0; } $SF = $filedir. " /". $filename; $FP =fopen ($SF, "w"); Write way Open file return fwrite ($fp, $infomation); Deposit Content fclose ($FP); Close file}//Create directory Private Function mkdirs ($dir) {if (!is_dir ($dir)) {if (! $this->mkdirs (dirname ($di R)) {return false; } if (!mkdir ($dir, 0777)) {return false; }} return true; }}new analytical ();
The above is the process of getting an IMG connection address for an HTML page.
Two more important things to use:
Dom parsing extension for 1.PHP Simplehtmldom
Directory iterators for 2.PHP
Understanding of these two things. There is no difficulty with this analysis class.
What if we get a page that needs parsing?
In fact, the principle is the same as above. Basically gets the URL of the page, then reads the page through curl, returns an HTML string,
The Save Function Package HTML page is saved locally.
I am here because I want to collect the picture inside the page (in order to prevent others anti-theft chain), so the design is more complex.
And why separate, because the Simplehtmldom object is very large, and open up so that the process clearer.
Some people will say, why not use the regular to match to skip the HTML saved to the local link, bingo! I'm just too lazy to write the regular.