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 相关文章推荐
POSIX 风格和兼容 Perl 风格两种正则表达式主要函数的类比(preg_match, preg_replace, ereg, ereg_replace)
Oct 12 PHP
PHP中函数rand和mt_rand的区别比较
Dec 26 PHP
关于crontab的使用详解
Jun 24 PHP
php构造函数实例讲解
Nov 13 PHP
php模板原理讲解
Nov 13 PHP
PHP查询快递信息的方法
Mar 07 PHP
PHP页面间传递值和保持值的方法
Aug 24 PHP
PHP获取IP地址所在地信息的实例(使用纯真IP数据库qqwry.dat)
Nov 15 PHP
laravel 框架配置404等异常页面
Jan 07 PHP
PHP实现的权重算法示例【可用于游戏根据权限来随机物品】
Feb 15 PHP
PHP使用Redis实现Session共享的实现示例
May 12 PHP
PHP SESSION跨页面传递失败解决方案
Dec 11 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
Php+SqlServer实现分页显示
2006/10/09 PHP
php simplexmlElement操作xml的命名空间实现代码
2011/01/04 PHP
迅速确定php多维数组的深度的方法
2014/01/07 PHP
php中动态修改ini配置
2014/10/14 PHP
PHP实现求连续子数组最大和问题2种解决方法
2017/12/26 PHP
javascript合并表格单元格实例代码
2016/01/03 Javascript
JavaScript+html5 canvas实现本地截图教程
2020/04/16 Javascript
Vue中添加过渡效果的方法
2017/03/16 Javascript
vue监听滚动事件实现滚动监听
2017/04/11 Javascript
js 事件的传播机制(实例讲解)
2017/07/20 Javascript
AngularJS中filter的使用实例详解
2017/08/25 Javascript
React native ListView 增加顶部下拉刷新和底下点击刷新示例
2018/04/27 Javascript
通过一次报错详细谈谈Point事件
2018/05/17 Javascript
layer弹出层 iframe层去掉滚动条的实例代码
2018/08/17 Javascript
vue实现父子组件之间的通信以及兄弟组件的通信功能示例
2019/01/29 Javascript
JS实现的检验身份证格式并输出出生日期,年龄,性别,出生地示例
2019/05/17 Javascript
深入了解JavaScript 防抖和节流
2019/09/12 Javascript
基于js实现逐步显示文字输出代码实例
2020/04/02 Javascript
微信小程序实现菜单左右联动
2020/05/19 Javascript
js 数据类型判断的方法
2020/12/03 Javascript
深入浅析python定时杀进程
2016/06/06 Python
PyChar学习教程之自定义文件与代码模板详解
2017/07/17 Python
django使用html模板减少代码代码解析
2017/12/12 Python
利用python将json数据转换为csv格式的方法
2018/03/22 Python
在pycharm下设置自己的个性模版方法
2019/07/15 Python
基于K.image_data_format() == 'channels_first' 的理解
2020/06/29 Python
Numpy中np.max的用法及np.maximum区别
2020/11/27 Python
美国奢侈品购物平台:Orchard Mile
2018/05/02 全球购物
若干个Java基础面试题
2015/05/19 面试题
涉外经济法专业毕业生推荐信
2013/11/24 职场文书
函授自我鉴定范文
2014/02/06 职场文书
思想品德评语大全
2014/12/31 职场文书
会计求职自荐信
2015/03/26 职场文书
护士旷工检讨书
2015/08/15 职场文书
2019年浪漫婚礼证婚词
2019/06/27 职场文书
MySQL分库分表与分区的入门指南
2021/04/22 MySQL