file_get_contents函数不能使用的解决方法
来源:互联网 发布:淘宝中评怎么改差评 编辑:程序博客网 时间:2024/05/17 04:27
有些主机服务商把php的allow_url_fopen选项是关闭了,就是没法直接使用file_get_contents来获取远程web页面的内容(采集和小偷程序常用)。那就是可以使用另外一个函数curl。
下面是file_get_contents和curl两个函数同样功能的不同写法
file_get_contents函数的使用示例:
PHP代码
- < ?php
- $file_contents = file_get_contents('http://www.helloks.com/');
- echo $file_contents;
- ?>
换成curl函数的使用示例:
PHP代码
- < ?php
- $ch = curl_init();
- $timeout = 5;
- curl_setopt ($ch, CURLOPT_URL, 'http://www.helloks.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代码
- < ?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都关闭了,上面的函数就会出现错误。
- file_get_contents函数不能使用的解决方法
- file_get_contents函数不能使用的解决方法
- file_get_contents函数不能使用的解决方法
- file_get_contents 的使用
- PHP中file_get_contents、fp_open等不能使用!
- file_get_contents无法请求https连接的解决方法
- file_get_contents无法请求https连接的解决方法
- file_get_contents无法请求https连接的解决方法
- file_get_contents无法请求https连接的解决方法
- php 使用file_get_contents的问题
- PHP file_put_contents&file_get_contents的使用
- file_get_contents使用
- PHP file_get_contents 函数获取URL的处理
- MySQL函数不能创建的解决方法
- MySQL函数不能创建的解决方法
- MySQL函数不能创建的解决方法
- 学习php需要了解的点4 - PHP中file_get_contents函数抓取https地址出错的解决方法
- 关于linux下面的file_get_contents的使用
- Zigbee无线传感器网络平台的设计与实现
- Windows Embedded Standard开发初体验(四)
- 配合实例学习php正则的使用
- 关于MS Sql Server性能问题
- SYBASE数据库安装的优化
- file_get_contents函数不能使用的解决方法
- 欢迎大家指教
- Eclipse+php插件+Xdebug搭建PHP完美开发/调试环境指南
- dns cache漏洞利用动画
- PHP 数字左侧自动补0
- htaccess文件的功能写法
- 枚举串口
- [总结]C与Shell程序的交互
- 如何动态控制LinkButton的PostBack