不用file_get_contents,用curl时。接口调用数据获取
来源:互联网 发布:php高级工程师 编辑:程序博客网 时间:2024/06/05 18:38
有些主机服务商把php的allow_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_setopt ($ch, CURLOPT_URL, 'http://www.ccvita.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都关闭了,上面的函数就会出现错误。
0 0
- 不用file_get_contents,用curl时。接口调用数据获取
- php file_get_contents和curl请求接口并获取数据
- curl获取接口数据
- 使用curl、socket、file_get_contents提交数据和获取数据
- 使用curl、socket、file_get_contents提交数据和获取数据
- 用curl 代替file_get_contents 解决获取网络资源的超时问题
- 用curl post 调用接口
- 接口的调用—CURL抓取数据
- file_get_contents,fopen,curl 获取远程页面
- php curl请求接口并获取数据
- php curl请求接口并获取数据
- php curl请求接口并获取数据
- PHP利用Curl、socket、file_get_contents POST数据
- 利用Curl、socket、file_get_contents POST数据
- PHP - 利用Curl、socket、file_get_contents POST数据
- 利用Curl、socket、file_get_contents POST数据
- PHP利用Curl、socket、file_get_contents POST数据
- php 调试微信接口时curl无返回值,file_get_contents有返回值的解决方法
- android----对listview设置动画
- 【Hadoop】HBase部署入门指南
- php即时聊天(反ajax)
- 组装字符串
- stm8s_STVD生成interrupt_vector.c问题
- 不用file_get_contents,用curl时。接口调用数据获取
- 2016——让一切都终结
- 关于日期处理的工具类
- 2 分布式数据库系统的结构
- 带你玩转Visual Studio
- Jetson_TK1_TX1学习网站
- eclipse常用快捷键
- 简单四则运算计算器的C++实现(含括号和+-*/的优先级判断)
- ExploitExercises_Nebula_Level14