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中10个不常见却非常有用的函数
Mar 21 PHP
解析在zend Farmework下如何创立一个FORM表单
Jun 28 PHP
php解析url的三个示例
Jan 20 PHP
PHP static局部静态变量和全局静态变量总结
Mar 02 PHP
php5.3以后的版本连接sqlserver2000的方法
Jul 28 PHP
php实现可用于mysql,mssql,pg数据库操作类
Dec 13 PHP
smarty内置函数{loteral}、{ldelim}和{rdelim}用法实例
Jan 22 PHP
判断、添加和删除WordPress置顶文章的相关PHP函数小结
Dec 10 PHP
PHPCMS V9 添加二级导航的思路详解
Oct 20 PHP
laravel学习笔记之模型事件的几种用法示例
Aug 15 PHP
PHP与Perl之间知识点区别整理
Mar 19 PHP
tp5.1 框架join方法用法实例分析
May 26 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自动加载autoload机制示例分享
2014/02/20 PHP
ecshop实现smtp发送邮件
2015/02/03 PHP
非常全面的php日期时间运算汇总
2015/11/04 PHP
PHP 7的一些引人注目的新特性简单介绍
2015/11/08 PHP
PHP一致性hash分布式算法封装类定义与用法示例
2018/08/04 PHP
firefox中JS读取XML文件
2006/12/21 Javascript
jquery 图片轮换效果
2010/07/29 Javascript
PHP 与 js的通信(via ajax,json)
2010/11/16 Javascript
JavaScript实现级联菜单的方法
2015/06/29 Javascript
Bootstrap学习笔记之css样式设计(1)
2016/06/07 Javascript
ECMAScript6轮播图实践知识总结
2016/08/17 Javascript
JavaScript学习笔记之惰性函数示例详解
2017/08/27 Javascript
Angular4自制一个市县二级联动组件示例
2017/11/21 Javascript
原生js+cookie实现购物车功能的方法分析
2017/12/21 Javascript
基于vue开发的在线付费课程应用过程
2018/01/25 Javascript
angularjs下ng-repeat点击元素改变样式的实现方法
2018/09/12 Javascript
vue使用Google地图的实现示例代码
2018/12/19 Javascript
微信小程序实现的一键连接wifi功能示例
2019/04/24 Javascript
node.js中Buffer缓冲器的原理与使用方法分析
2019/11/23 Javascript
Vue filter 过滤当前时间 实现实时更新效果
2019/12/20 Javascript
Python多线程、异步+多进程爬虫实现代码
2016/02/17 Python
python生成器表达式和列表解析
2016/03/10 Python
Python3多线程爬虫实例讲解代码
2018/01/05 Python
实例详解python函数的对象、函数嵌套、名称空间和作用域
2019/05/31 Python
Django的用户模块与权限系统的示例代码
2019/07/24 Python
一篇文章搞定Python操作文件与目录
2019/08/13 Python
详解Python图像处理库Pillow常用使用方法
2019/09/02 Python
Anaconda的安装及其环境变量的配置详解
2020/04/22 Python
详解Python设计模式之策略模式
2020/06/15 Python
美国的Eastbay旗下的运动款子品牌:Final-Score
2018/01/01 全球购物
匈牙利超级网上商店和优惠:Alza.hu
2019/12/17 全球购物
教师年度考核自我鉴定
2014/01/19 职场文书
集中采购方案
2014/06/10 职场文书
2014年审计工作总结
2014/11/17 职场文书
nginx location优先级的深入讲解
2021/03/31 Servers
Java中PriorityQueue实现最小堆和最大堆的用法
2021/06/27 Java/Android