php如何获得网页内容,读取远处文件,几个方法比较下

使用php读取远程文件有4种方式:

fopen()
file_get_contents()
curl函数
socket函数
fopen()与file_get_contents()需要在php.ini配置文件中激活allow_url_fopen选项。

fopen()方式

$handle = fopen (“http://www.limingzhao.com/”, “rb”);

$contents = “”;

while (!feof($handle)) {

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

}

fclose($handle);

file_get_contents()方式

$contents = file_get_contents(‘http://www.limingzhao.com/’);
curl函数

if (function_exists(‘curl_init’)){

$ch=curl_init();

curl_setopt($ch, CURLOPT_URL, ‘http://www.limingzhao.com’);

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_USERAGENT, ‘Mozilla/5.0 (Windows; U; Windows NT 5.1; )’);

$content=curl_ex ec($ch);

curl_close($ch);

}

socket方式

function getRemoteFile($url)

{

$parsedUrl = parse_url($url);

$host = $parsedUrl['host'];

if (isset($parsedUrl['path'])) {

$path = $parsedUrl['path'];

} else {

$path = ‘/’;

}



if (isset($parsedUrl['query'])) {

$path .= ‘?’ . $parsedUrl['query'];

}



if (isset($parsedUrl['port'])) {

$port = $parsedUrl['port'];

} else {

$port = ’80′;

}



$timeout = 10;

$response = ”;

$fp = @fsockopen($host, ’80′, $errno, $errstr, $timeout );

if( !$fp ) {

echo “连接$url失败”;

} else {

fputs($fp, “GET $path HTTP/1.0\r\n” .
“Host: $host\r\n” .
“User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1;) \r\n” .
“Accept: */*\r\n” .
“Accept-Language: zh-CN,zh;q=0.5\r\n” .
“Accept-Charset: GB-2312,utf-8;q=0.7,*;q=0.7\r\n” .
“Keep-Alive: 300\r\n” .
“Connection: keep-alive\r\n” .

“Referer: http://$host\r\n\r\n”);



while ( $line = fread( $fp, 4096 ) ) {

$response .= $line;

}



fclose( $fp );

$pos = strpos($response, “\r\n\r\n”);

$response = substr($response, $pos + 4);

}



return $response;

}


PHP读取远程文件的三种方法
做采集的第一步就是读取远程文件…

1.file_get_contents

PHP代码

$url = http://www.limingzhao.com/index.php?q=node/169;

$contents = file_get_contents($url);

//如果出现中文乱码使用下面代码

//$getcontent = iconv(”gb2312″, “utf-8″,file_get_contents($url));

//echo $getcontent;

echo $contents;

?>

2.curl

PHP代码

$url = “http://www.limingzhao.com/index.php?q=node/169”;

$ch = curl_init();

$timeout = 5;

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

//在需要用户检测的网页里需要增加下面两行

//curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);

//curl_setopt($ch, CURLOPT_USERPWD, US_NAME.”:”.US_PWD);

$contents = curl_exec($ch);

curl_close($ch);

echo $contents;

?>

3.fopen->fread->fclose

PHP代码

$handle = fopen (”http://www.limingzhao.com/index.php?q=node/169”, “rb”);

$contents = “”;

do {

$data = fread($handle, 8192);

if (strlen($data) == 0) {

break;

}

$contents .= $data;

} while(true);

fclose ($handle);

echo $contents;

?>

Ps1. 使用file_get_contents和fopen必须空间开启allow_url_fopen。方法:编辑php.ini,设置 allow_url_fopen = On,allow_url_fopen关闭时fopen和file_get_contents都不能打开远程文件。

Ps2.使用curl必须空间开启curl。方法:WIN下修改php.ini,将extension=php_curl.dll前面的分号去掉, 而且需要拷贝ssleay32.dll和libeay32.dll到C:\WINDOWS\system32下;Linux下要安装curl扩展。

建议打开URL时使用file_get_contents()方法,可优化打开速度

PHP读取远程文件用fopen比file更稳定
我们在PHP编程时,常常要读取远程文件,但用fopen和file都能实现,那使用那个效率高些呢?

经测试

$handle=fopen($filename,wb); flock($handle,LOCK_SH);



$filedata=fread($handle,filesize($filename));



fclose($handle);


的效率要比
file($filename);
无论在速度还是稳定上都要优秀,大家不妨试试。