PHP 获取远程文件内容的函数代码


Posted in PHP onMarch 24, 2010

如下函数:

<? 
/** 
获取远程文件内容 
@param $url 文件http地址 
*/ 
function fopen_url($url) 
{ 
if (function_exists('file_get_contents')) { 
$file_content = @file_get_contents($url); 
} elseif (ini_get('allow_url_fopen') && ($file = @fopen($url, 'rb'))){ 
$i = 0; 
while (!feof($file) && $i++ < 1000) { 
$file_content .= strtolower(fread($file, 4096)); 
} 
fclose($file); 
} elseif (function_exists('curl_init')) { 
$curl_handle = curl_init(); 
curl_setopt($curl_handle, CURLOPT_URL, $url); 
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT,2); 
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER,1); 
curl_setopt($curl_handle, CURLOPT_FAILONERROR,1); 
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Trackback Spam Check'); //引用垃圾邮件检查 
$file_content = curl_exec($curl_handle); 
curl_close($curl_handle); 
} else { 
$file_content = ''; 
} 
return $file_content; 
} 
?>

相关解释:
1,ini_get : Returns the value of the configuration option as a string on success, or an empty string on failure(读取 php.ini 配置文件中的值)
2,; Whether to allow the treatment of URLs (like http:// or ftp://) as files.
allow_url_fopen = On(配置文件中的内容)
3,fopen( "rb"): 在操作二进制文件时如果没有指定 'b' 标记,可能会碰到一些奇怪的问题,包括坏掉的图片文件以及关于 \r\n 字符的奇怪问题。
注意: 为移植性考虑,强烈建议在用 fopen() 打开文件时总是使用 'b' 标记。
注意: 再一次,为移植性考虑,强烈建议你重写那些依赖于 't' 模式的代码使其使用正确的行结束符并改成 'b' 模式。
4,strtolower -- Make a string lowercase
5,curl_init() :curl_init -- Initialize a cURL session(初始化一个cUrl会话)
resource curl_init ( [string url] )
Initializes a new session and return a cURL handle for use with the curl_setopt(), curl_exec(), and curl_close() functions.
url--If provided, the CURLOPT_URL option will be set to its value. You can manually set this using the curl_setopt() function.
Returns a cURL handle on success, FALSE on errors.
6,curl_setopt -- Set an option for a cURL transfer(提供设置)
bool curl_setopt ( resource ch, int option, mixed value )
Sets an option on the given cURL session handle. (具体请看 PHP 手册) There:
CURLOPT_URL :The URL to fetch. You can also set this when initializing a session with curl_init().
CURLOPT_CONNECTTIMEOUT :The number of seconds to wait whilst trying to connect. Use 0 to wait indefinitely.(无限期等待 设置为 0)
CURLOPT_RETURNTRANSFER :TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
CURLOPT_FAILONERROR :TRUE to fail silently if the HTTP code returned is greater than or equal to 400. The default behavior is to return the page normally, ignoring the code.
CURLOPT_USERAGENT :The contents of the "User-Agent: " header to be used in a HTTP request.
7,curl_exec : Perform a cURL session, This function should be called after you initialize a cURL session and all the options for the session are set.
如果成功则返回 TRUE,失败则返回 FALSE。 However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure
8,curl_close -- Close a cURL session

下面是一些参考代码:
PHP 采集程序 常用函数
PHP 采集获取指定网址的内容

PHP 相关文章推荐
PHP 变量定义和变量替换的方法
Jul 30 PHP
PHP 数组教程 定义数组
Oct 23 PHP
php垃圾代码优化操作代码
Aug 05 PHP
PHP中使用数组实现堆栈数据结构的代码
Feb 05 PHP
PHP中全局变量global和$GLOBALS[]的区别分析
Aug 06 PHP
php采集自中央气象台范围覆盖全国的天气预报代码实例
Jan 04 PHP
php模板引擎技术简单实现
Mar 15 PHP
PHP构造函数与析构函数用法示例
Sep 28 PHP
linux平台编译安装PHP7并安装Redis扩展与Swoole扩展实例教程
Sep 30 PHP
php实现的XML操作(读取)封装类完整实例
Feb 23 PHP
Laravel Reponse响应客户端示例详解
Sep 03 PHP
is_file和file_exists效率比较
Mar 14 PHP
PHP中基本符号及使用方法
Mar 23 #PHP
PHP技术开发技巧分享
Mar 23 #PHP
PHP初学者常见问题集合 修正版(21问答)
Mar 23 #PHP
PHP5 字符串处理函数大全
Mar 23 #PHP
Smarty Foreach 使用说明
Mar 23 #PHP
用php或asp创建网页桌面快捷方式的代码
Mar 23 #PHP
php 无限级分类学习参考之对ecshop无限级分类的解析 带详细注释
Mar 23 #PHP
You might like
JS实现php的伪分页
2008/05/25 PHP
PHP学习之输出字符串(echo,print,printf,print_r和var_dump)
2011/04/17 PHP
2款PHP无限级分类实例代码
2015/11/11 PHP
微信支付扫码支付php版
2016/07/22 PHP
PHPUnit + Laravel单元测试常用技能
2019/11/06 PHP
javascript中HTMLDOM操作详解
2014/12/11 Javascript
网页收藏夹显示ICO图标(代码少)
2015/08/04 Javascript
属于你的jQuery提示框(Tip)插件
2016/01/20 Javascript
基于Vue.js的表格分页组件
2016/05/22 Javascript
bootstrap导航条实现代码
2016/12/28 Javascript
在create-react-app中使用css modules的示例代码
2018/07/31 Javascript
基于vue实现圆形菜单栏组件
2019/07/05 Javascript
django简单的前后端分离的数据传输实例 axios
2020/05/18 Javascript
vuex中store存储store.commit和store.dispatch的用法
2020/07/24 Javascript
[03:04]2018年度DOTA2玩家最喜爱的主播-完美盛典
2018/12/16 DOTA
[05:11]TI9战队采访——VIRTUSPRO
2019/08/22 DOTA
go和python调用其它程序并得到程序输出
2014/02/10 Python
Python 爬虫爬取指定博客的所有文章
2016/02/17 Python
详解 Python 读写XML文件的实例
2017/08/02 Python
python自动裁剪图像代码分享
2017/11/25 Python
OpenCV+python手势识别框架和实例讲解
2018/08/03 Python
Python3.5基础之变量、数据结构、条件和循环语句、break与continue语句实例详解
2019/04/26 Python
Python 实现输入任意多个数,并计算其平均值的例子
2019/07/16 Python
2021年值得向Python开发者推荐的VS Code扩展插件
2021/01/25 Python
布里斯班女装时尚品牌:Adrift
2017/12/28 全球购物
怎样创建、运行java程序
2014/08/01 面试题
Oracle性能调优原则
2012/05/03 面试题
Ruby如何创建一个线程
2013/03/10 面试题
2014年大学生党员自我评议
2014/09/22 职场文书
私人房屋买卖协议书
2014/10/04 职场文书
企业法人任命书
2015/09/21 职场文书
《金色的草地》教学反思
2016/02/17 职场文书
Nginx快速入门教程
2021/03/31 Servers
Go遍历struct,map,slice的实现
2021/06/13 Golang
Go语言实现一个简单的并发聊天室的项目实战
2022/03/18 Golang
js前端图片加载异常兜底方案
2022/06/21 Javascript