专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »PHP教程 » filegetcontents:file_get_contents函数不能使用的解决思路方法 »正文

filegetcontents:file_get_contents函数不能使用的解决思路方法

来源: 发布时间:星期一, 2009年1月12日 浏览:32次 评论:0
  有些主机服务商把phpallow_url_fopen选项是关闭了就是没法直接使用file_get_contents来获取远程web页面内容那就是可以使用另外curl

  下面是file_get_contents和curl两个同样功能区别写法

  file_get_contents使用举例:

< ?php
$file_contents = file_get_contents('http://www.ccvita.com/');
echo $file_contents;
?>


  换成curl使用举例:

< ?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来判断php是否支持可以轻松写出下面

< ?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;
}
?>
其实上面这个还有待商榷如果你主机服务商把file_get_contents和curl都关闭了上面就会出现

0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: