您的位置:首页 > 编程语言 > PHP开发

php抓取网页代码四种方法

2010-09-09 20:52 666 查看
PHP抓取页面代码四种 方法:
1. file()函数
2. file_get_contents()函数
3. fopen()->fread()->fclose()模式
4.curl方式
file()方法:
<?php
$url='http://t.qq.com';
$lines_array=file($url);
$lines_string=implode('',$lines_array);
echo htmlspecialchars($lines_string);
?>
file_get_contents()方法:
<?php
$url='http://t.qq.com';
$lines_string=file_get_contents($url);
echo htmlspecialchars($lines_string);
?>
fopen()->fread()->fclose()模式
<?php
$url='http://t.qq.com';
$handle=fopen($url,"rb");
$lines_string="";
do{
$data=fread($handle,1024);
if(strlen($data)==0){break;}
$lines_string.=$data;
}while(true);
fclose($handle);
echo htmlspecialchars($lines_string);
?>
CURL方法:
<?php
$url='http://t.qq.com';
$ch=curl_init();
$timeout=5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$lines_string=curl_exec($ch);
curl_close($ch);
echo htmlspecialchars($lines_string);
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: