php 读取一个网页的内容,PHP读取网页文件内容的实现代码(fopen,_php

1.fopen实现代码:

复制代码 代码如下:

$handle = fopen ("http://www.example.com/", "rb");

$contents = "";

while (!feof($handle)) {

http://www.gaodaima.com/?p=49362PHP读取网页文件内容的实现代码(fopen,_php

$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.net");

$html = iconv( "Big5", "UTF-8//IGNORE" , $html); //转化编码方式为UTF8

print $html;

$htm = file("http://s.jb51.net");

$h = "";

foreach($htm as $value)

{

$h.= iconv( "GB2312", "utf-8//IGNORE" , $value);

}

print_r($h);

另一种打开网页的方法

复制代码 代码如下:

$opts = array(

'http'=>array(

'method'=>"GET",

'header'=>"Accept-language: en/r/n" .

"Cookie: foo=bar/r/n"

)

);

$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);

?>

欢迎大家阅读《PHP读取网页文件内容的实现代码(fopen,_php》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码

e7ce419cf2d6ad34d01da2ceb8829eed.png

微信 赏一包辣条吧~

023a57327877fb4402bcc76911ec18ea.png

支付宝 赏一听可乐吧~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值