php抓取网页内容的方法
转自:?http://bbs.phplovers.com/read-htm-tid-453.html
1、file_get_contents:
?
立即学习“PHP免费学习笔记(深入)”;
<?php$url = "http://www.phpzixue.cn"; $contents = file_get_contents($url); //如果出现中文乱码使用下面代码 //$getcontent = iconv("gb2312", "utf-8",$contents); echo $contents; ?>
?
立即学习“PHP免费学习笔记(深入)”;
?
立即学习“PHP免费学习笔记(深入)”;
2、curl:
?
立即学习“PHP免费学习笔记(深入)”;
<?php$url = "http://www.phpzixue.cn";$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; ?>
?
立即学习“PHP免费学习笔记(深入)”;
?
立即学习“PHP免费学习笔记(深入)”;
3、fopen->fread->fclose:
?
立即学习“PHP免费学习笔记(深入)”;
<?php$handle = fopen ("http://www.phpzixue.cn", "rb"); $contents = ""; do { $data = fread($handle, 1024); if (strlen($data) == 0) { break; } $contents .= $data; } while(true); fclose ($handle); echo $contents; ?>
?
立即学习“PHP免费学习笔记(深入)”;
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号