php中file_get_contents和curl两个函数用法
发布:smiling 来源: PHP粉丝网 添加日期:2014-08-02 10:29:26 浏览: 评论:0
文章简单的介绍了php中file_get_contents和curl两个函数用法,在不能使用file_get_contents时可以尝试一下curl函数,下面是file_get_contents和curl两个函数同样功能的不同写法.
file_get_contents函数的使用示例,代码如下:
- <?php
 - $file_contents = file_get_contents('http://www.phpfensi.com/');
 - echo $file_contents;
 - ?>
 
换成curl函数的使用示例,代码如下:
- <?php
 - $ch = curl_init();
 - $timeout = 5;
 - curl_setopt ($ch, CURLOPT_URL, 'http://www.phpfensi.com');
 - curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
 - curl_setopt ($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) {
 - if(function_exists('file_get_contents')) {
 - $file_contents = file_get_contents($url);
 - } else {
 - $ch = curl_init();
 - $timeout = 5;
 - curl_setopt ($ch, CURLOPT_URL, $url);
 - curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
 - curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
 - $file_contents = curl_exec($ch);
 - curl_close($ch);
 - }
 - return $file_contents;
 - }
 - ?>
 
其实上面的这个函数还有待商议,如果你的主机服务商把file_get_contents和curl都关闭了,上面的函数就会出现错误.
Tags: file_get_contents curl 函数
- 上一篇:php中博客日历实现代码
 - 下一篇:PHP中英数字混排字符串的截取
 
相关文章
- ·php中file_get_contents获取网页乱码解决办法(2013-12-05)
 - ·php file_get_contents与curl()函数对比(2014-01-16)
 - ·php file_get_contents数据采集与常用见问题解决(2014-01-16)
 - ·php中常用文件操作读写函数介绍(2014-03-28)
 - ·php中curl和file_get_content函数抓页面对比(2014-06-21)
 - ·file_get_contents实现数据Post数据方法(2014-08-04)
 - ·file_get_contents获取远程网页内容函数(2014-09-20)
 - ·php中file_get_contents代替使用curl示例(2015-04-09)
 - ·PHP CURL或file_get_contents获取网页标题的代码(2015-04-10)
 - ·php中file_get_contents函数高级用法(2015-04-15)
 - ·PHP file_get_contents函数读取远程数据超时的解决方法(2021-05-26)
 - ·php中file_get_contents()函数用法实例(2021-11-10)
 - ·php curl_init函数用法(2013-11-28)
 - ·php使用curl函数提示Call to undefined function curl_init()(2013-12-04)
 - ·php提示:Call to undefined function curl_init(2013-12-04)
 - ·cURL函数库错误码说明之PHP curl_errno函数(2013-12-05)
 
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
 - PHP新手上路(一)(7)
 - 惹恼程序员的十件事(5)
 - PHP邮件发送例子,已测试成功(5)
 - 致初学者:PHP比ASP优秀的七个理由(4)
 - PHP会被淘汰吗?(4)
 - PHP新手上路(四)(4)
 - 如何去学习PHP?(2)
 - 简单入门级php分页代码(2)
 - php中邮箱email 电话等格式的验证(2)
 
