手机
当前位置:查字典教程网 >编程开发 >php教程 >PHP读取网页文件内容的实现代码(fopen,curl等)
PHP读取网页文件内容的实现代码(fopen,curl等)
摘要:1.fopen实现代码:复制代码代码如下:复制代码代码如下:2.curl实现代码:复制代码代码如下:编码转换函数复制代码代码如下:$html...

1.fopen实现代码:

复制代码 代码如下:

<?php

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

$contents = "";

while (!feof($handle)) {

$contents .= fread($handle, 8192);

}

fclose($handle);

?>

复制代码 代码如下:

<?php

// 对 PHP 5 及更高版本

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

$contents = stream_get_contents($handle);

fclose($handle);

?>

2.curl实现代码:

复制代码 代码如下:

<?php

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

另一种打开网页的方法

复制代码 代码如下:

<?php

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

?>

【PHP读取网页文件内容的实现代码(fopen,curl等)】相关文章:

我的论坛源代码(九)

我的论坛源代码(十)

PHP文件读取功能的应用实例

php获取网页里所有图片并存入数组的方法

我的论坛源代码(六)

用PHP实现文件上传

PHP个人网站架设连环讲(一)

PHP实现远程下载文件到本地

留言板翻页的实现详解

文件上传的实现

精品推荐
分类导航