配色: 字号:
PHP读取网页文件内容的实现代码(fopen,curl等)
2016-10-28 | 阅:  转:  |  分享 
  
php小偷程序中经常需要获取远程网页的内容,下面是一些实现代码,需要的朋友可以惨况下。1.fopen实现代码:复制代码代码如下:$handle=fopen("http://www.example.com/","rb");?$contents=""
;?while(!feof($handle)){?$contents.=fread($handle,8192);?}?f
close($handle);??>?复制代码代码如下:n("http://www.example.com/","rb");?$contents=stream_get_conten
ts($handle);?fclose($handle);??>?2.curl实现代码:复制代码代码如下:on_url($Date){?$ch=curl_init();?$timeout=5;?curl_setopt($ch
,CURLOPT_URL,"$Date");?curl_setopt($ch,CURLOPT_RETURNTRANSFER
,1);?curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/4.0(compatib
le;MSIE6.0;WindowsNT5.1;SV1)");?curl_setopt($ch,CURLOPT_C
ONNECTTIMEOUT,$timeout);?$contents=curl_exec($ch);?curl_close(
$ch);?return$contents;?}?$pageURL="http://www.baidu.com";?$conte
nts=_url($pageURL);??>?编码转换函数复制代码代码如下:$html=file_get_contents("
http://s.jb51.net");?$html=iconv("Big5","UTF-8//IGNORE",$ht
ml);//转化编码方式为UTF8?print$html;?$htm=file("http://s.jb51.net");
?$h="";?foreach($htmas$value)?{?$h.=iconv("GB2312","utf-8/
/IGNORE",$value);?}?print_r($h);?另一种打开网页的方法?复制代码代码如下:s=array(?''http''=>array(?''method''=>"GET",?''header''=>"Accept-lang
uage:en\r\n".?"Cookie:foo=bar\r\n"?)?);?$context=stream_cont
ext_create($opts);?/Sendsanhttprequesttowww.mntuku.cn?with
additionalheadersshownabove/?$fp=fopen(''http://www.baidu.
com'',''r'',false,$context);?fpassthru($fp);?fclose($fp);??>?
献花(0)
+1
(本文系雨亭之东首藏)