PHP实现小偷程序
優點:通過PHP小偷,我們可以借用別人網站上的信息而不用自己辛苦地去采集。?
缺點:需要把整頁的HTML代碼讀取到本地,然后通過匹配獲取到想要的信息再進行顯示,這樣大大影響網頁的加載速度。?
改進:通過AJAX異步讀取HTML代碼,后臺進行匹配獲取到想要的信息,再顯示到前臺頁面。?
預備知識:PHP、AJAX、正則表達式。?
關鍵字:file_get_contents()、fopen()、fwrite()、eregi(),前三個關鍵字都是關于PHP文件處理的函數,eregi()是關于正則表達式的函數。?
一、PHP小偷原理的實現:
1、file_get_contents()、fopen()、fwrite()的應用?
咱們用華夏黑客牛刀小試吧,把主頁的全部信息獲取過來。?
建立一個PHP文件:?
| <?php? $url='http://www.77169.com';? //file_get_contents()函數獲取網頁的html文檔? $file=file_get_contents($url);? //建立一個新文件ImitationIndex.htm? $newfile=fopen('ImitationIndex.htm','w');? //把內容讀取進去? fwrite($newfile,$file);? //關閉打開的文件? fclose($newfile);? ?> |
ImitationIndex.htm獲取主頁全部的HTML(保存好,以備下次使用)。?
2、eregi()函數的應用?
通過eregi()獲取里的內容:?
| <?php? $url='http://www.77169.com';? //file_get_contents()函數獲取網頁的html文檔? $file=file_get_contents($url);? //通過eregi()匹配獲取想要的信息? eregi('<title>(.*)</title>',$file,$rg);? //建立一個新文件? $newfile=fopen('title.htm','w');? //把內容讀取進去? fwrite($newfile,$rg[1]);? ?> |
?
打開title.htm可以看到已經獲取了<title></title>之間的內容?
從上面的兩個例子,已經知道PHP小偷的實現原理,從中也知道它的不足之處,但通過AJAX可以完善它,使它可以用于實踐項目中。?
二、PHP小偷技術的實用和改進:?
目的:獲取華夏黑客的最新資訊,并實時更新查看示例。?
實際中可能只用到三個頁面:showNews.php、Update.php、HackNews.htm,但為了能看到實時更新的效果,這里用到了模擬主頁的頁面ImitationIndex.htm。?
showNews.php:?
| <style?type="text/css">? body{background:#c60;}? a{color:#444;font:12px?Courier?New;padding:8px;}? a:hover{color:#f00}? #hei_content{width:400px;background:#ccc;border:2px?solid?#000;border-bottom:5px?solid?#000;}? </style>? <?php? echo('<div?id="hei_content">');? include('HackNews.htm');? echo('</div>');? ?>? <script?type="text/javascript">? window.οnlοad=UpdateNews;? function?GetXmlHttp()? {? var?xmlhttp;? try? {? //IE7.0?ect? xmlhttp=new?ActiveXObject("Msxml2.XMLHTTP");? }? catch(a)? {? try? {? //ie6.0?ect? xmlhttp=new?ActiveXObject("Mscrosoft.XMLHTTP");? }? catch(b)? {? //非IE? xmlhttp=new?XMLHttpRequest();? }? }? return?xmlhttp;? }? function?StateChange()? {? if(xmlhttp.readyState==4&&xmlhttp.status==200)? {? document.getElementById("hei_content").innerHTML=xmlhttp.responseText;? }? }? function?UpdateNews()? {? xmlhttp=GetXmlHttp();? xmlhttp.onreadystatechange=StateChange;? var?url='Update.php';? url=url+"?sid="+Math.random();? xmlhttp.open("GET",url,true);? xmlhttp.send(null);? }? </script> |
HackNews.htm是已經緩存的文件,這樣不用加載主頁上的整個HTML再顯示,這個工作留給AJAX:?
| document.getElementById("hei_content").innerHTML=xmlhttp.responseText |
xmlhttp.responseText就是加載更新后的內容。?
Update.php:?
| <?php? $url='ImitationIndex.htm';? //file_get_contents()函數獲取網頁的html文檔? $file=file_get_contents($url);? //$regx為正則表達式的內容,用于匹配獲取想要的信息? $regx='<td?background=http://hack.77169.com/UploadFiles_8057/200902/20090220100422648.gif?colSpan=3?height=100>? (.*)</td>? </tr>? </table>? </td>? </tr>? <tr>? <td?colSpan=3><IMG?height=8?src=/"http://hack.77169.com/UploadFiles_8057/200902/20090220100422568.gif';? //eregi()獲取匹配到的內容,賦值給數組變量$rg;? eregi($regx,$file,$rg);? //建立一個新文件HackNews.htm? $newfile=fopen('HackNews.htm','w');? //把內容讀取進去? fwrite($newfile,$rg[1]);? fclose($newfile);? //用iconv編碼轉換后,輸出匹配的HTML內容? $rg[1]=iconv('gb2312','utf-8',$rg[1]);? echo($rg[1]);? ?> |
Update.php是異步更新的后臺頁面,“$newfile=fopen('HackNews.htm','w');”和 “fwrite($newfile,$rg[1]);”把更新的內容重新保存在HackNews.htm中,下次在index.php中include時 就直接調用這個頁面,“$url='ImitationIndex.htm'”在實際應用中應該是 “$url='http://www.77169.com'”,但為了便于調試,建立一個ImitationIndex.htm模擬頁面,如果對 ImitationIndex.htm頁面中最新資訊中的內容改改,就可以看到實時更新效果。?
總結:通過AJAX改進的PHP小偷技術,在采集多個網站的大量信息下,頁面的加載速度并不會受多大影響。
總結
- 上一篇: Java基础知识面试题(总结最全面的面试
- 下一篇: Java8 Stream接口流式方法:m