首页 »PHP教程 » filegetcontents:file_get_contents函数不能使用的解决思路方法 »正文
filegetcontents:file_get_contents函数不能使用的解决思路方法
来源: 发布时间:星期一, 2009年1月12日 浏览:32次 评论:0
有些主机服务商把php ![](/icons/78749de.gif) allow_url_fopen选项是关闭了 ![](/icons/78749dou.gif) 就是没法直接使用file_get_contents来获取远程web页面 ![](/icons/78749de.gif) 内容 ![](/icons/78749dou2.gif) 那就是可以使用另外 ![](/icons/78749yi.gif) 个 ![](/icons/78749hanshu.gif) curl ![](/icons/78749dou2.gif) 下面是file_get_contents和curl两个 ![](/icons/78749hanshu.gif) 同样功能 ![](/icons/78749de.gif) 区别写法 file_get_contents ![](/icons/78749hanshu.gif) ![](/icons/78749de.gif) 使用举例: < ?php $file_contents = file_get_contents('http://www.ccvita.com/'); echo $file_contents; ?> 换成curl ![](/icons/78749hanshu.gif) ![](/icons/78749de.gif) 使用举例: < ?php $ch = curl_init ; $timeout = 5; curl_ opt ($ch, CURLOPT_URL, 'http://www.ccvita.com'); curl_ opt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_ opt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $file_contents = curl_exec($ch); curl_close($ch); echo $file_contents; ?> 利用function_exists ![](/icons/78749hanshu.gif) 来判断php是否支持 ![](/icons/78749yi.gif) 个 ![](/icons/78749hanshu.gif) 可以轻松写出下面 ![](/icons/78749hanshu.gif) < ?php function vita_get_url_content($url) {
(function_exists('file_get_contents')) { $file_contents = file_get_contents($url); } { $ch = curl_init ; $timeout = 5; curl_ opt ($ch, CURLOPT_URL, $url); curl_ opt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_ opt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $file_contents = curl_exec($ch); curl_close($ch); }
$file_contents; } ?> 其实上面 ![](/icons/78749de.gif) 这个 ![](/icons/78749hanshu.gif) 还有待商榷 ![](/icons/78749dou.gif) 如果你 ![](/icons/78749de.gif) 主机服务商把file_get_contents和curl都关闭了 ![](/icons/78749dou.gif) 上面 ![](/icons/78749de.gif) ![](/icons/78749hanshu.gif) 就会出现 ![](/icons/78749cuowu.gif)
相关文章
读者评论
发表评论
|
|