加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

PHP读取网页文件内容的实现代码(fopen,curl等)

发布时间:2020-05-24 19:39:16 所属栏目:PHP 来源:互联网
导读:php小偷程序中经常需要获取远程网页的内容,下面是一些实现代码,需要的朋友可以惨况下。

1.fopen实现代码:

代码如下:$handle = fopen ("http://www.example.com/","rb");
$contents = "";
while (!feof($handle)) {
$contents .= fread($handle,8192);
}
fclose($handle);
?>

代码如下:// 对 PHP 5 及更高版本
$handle = fopen("http://www.example.com/","rb");
$contents = stream_get_contents($handle);
fclose($handle);
?>

2.curl实现代码:

代码如下:function _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 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
curl_setopt ($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$contents = curl_exec($ch);
curl_close($ch);
return $contents;
}
$pageURL="http://www.baidu.com";
$contents=_url($pageURL);
?>

编码转换函数
代码如下:$html = file_get_contents("http://s.jb51.cc");
$html = iconv( "Big5","UTF-8//IGNORE",$html); //转化编码方式为UTF8
print $html;
$htm = file("http://s.jb51.cc");
$h = "";
foreach($htm as $value)
{
$h.= iconv( "GB2312","utf-8//IGNORE",$value);
}
print_r($h);

另一种打开网页的方法

代码如下:$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: enrn" .
"Cookie: foo=barrn"
)
);
$context = stream_context_create($opts);
/* Sends an http request to www.example.com
with additional headers shown above */
$fp = fopen('http://www.baidu.com','r',false,$context);
fpassthru($fp);
fclose($fp);
?>

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读