Jul
20
2012
获取网页内容的方法吧 欢迎指教
//file_get_contents <?php $url = "http://www.120sc.com"; $contents = file_get_contents($url); //如果出现中文乱码使用下面代码 mb_convert_string(); //$getcontent = iconv("gb2312", "utf-8",$contents); echo $contents; ?> //curl <?php $url = "http://www.120sc.com"; $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; ?> //stream <?php $handle = fopen ("http://www.120sc.com", "rb"); $contents = ""; do { $data = fread($handle, 1024); if (strlen($data) == 0) { break; } $contents .= $data; } while(true); fclose ($handle); echo $contents; ?>
当然这只是初级的简单应用 举例
比如curl的话 博大精深我都没怎么搞彻底 一切玄机尽在curl_setopt()中
常用的就是模拟登陆了的 我所用过的就是一站式登陆 即所属站点的同步登陆与登出
用时须配置 php.ini中的curl模块
使用file_get_contents和fopen必须编辑php.ini,设置allow_url_fopen = On,allow_url_fopen关闭时fopen和file_get_contents都不能打开远程文件 哦 ,记得以前就吃过这个亏 ,小心
流文件读取主要在文件缓存和文件读取时使用
恩 差不多就这些了 呵呵
最活跃的读者