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 相关文章推荐
ThinkPHP php 框架学习笔记
Oct 30 PHP
约瑟夫环问题的PHP实现 使用PHP数组内部指针操作函数
Oct 12 PHP
探讨PHP中this,self,parent的区别详解
Jun 08 PHP
Yii框架调试心得--在页面输出执行sql语句
Dec 25 PHP
PHP+JQUERY操作JSON实例
Mar 23 PHP
PHP实现时间比较和时间差计算的方法示例
Jul 24 PHP
[原创]PHP获取数组表示的路径方法分析【数组转字符串】
Sep 01 PHP
PHP实现的多维数组排序算法分析
Feb 10 PHP
PHP学习记录之数组函数
Jun 01 PHP
YII框架http缓存操作示例
Apr 29 PHP
laravel 使用auth编写登录的方法
Sep 30 PHP
在Laravel中使用GuzzleHttp调用第三方服务的API接口代码
Oct 15 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下实现折线图效果的代码
2007/04/28 PHP
采用memcache在web集群中实现session的同步会话
2014/07/05 PHP
Yii入门教程之目录结构、入口文件及路由设置
2014/11/25 PHP
制作安全性高的PHP网站的几个实用要点
2014/12/30 PHP
Yii rules常用规则示例
2016/03/15 PHP
分析 JavaScript 中令人困惑的变量赋值
2007/08/13 Javascript
Javascript 读书笔记索引贴
2010/01/11 Javascript
jqGrid日期格式的判断示例代码(开始日期与结束日期)
2013/11/08 Javascript
解决json日期格式问题的3种方法
2014/02/02 Javascript
jQuery对象的selector属性用法实例
2014/12/27 Javascript
JavaScript中对象property的删除方法介绍
2014/12/30 Javascript
jquery实现平滑的二级下拉菜单效果
2015/08/26 Javascript
JS+CSS实现自动切换的网页滑动门菜单效果代码
2015/09/14 Javascript
学习vue.js计算属性
2016/12/03 Javascript
jquery删除数组中重复元素
2016/12/05 Javascript
bootstrap输入框组使用方法
2017/02/07 Javascript
10道典型的JavaScript面试题
2017/03/22 Javascript
详解vue2.0脚手架的webpack 配置文件分析
2017/05/27 Javascript
JavaScript数据结构与算法之二叉树插入节点、生成二叉树示例
2019/02/21 Javascript
微信小程序引入Vant组件库过程解析
2019/08/06 Javascript
Bootstrap table 实现树形表格联动选中联动取消功能
2019/09/30 Javascript
python3简单实现微信爬虫
2015/04/09 Python
在Python的Django框架中创建语言文件
2015/07/27 Python
利用python获取当前日期前后N天或N月日期的方法示例
2017/07/30 Python
Python字符串格式化f-string多种功能实现
2020/05/07 Python
python中的yield from语法快速学习
2020/11/06 Python
匈牙利墨盒和碳粉购买网站:CDRmarket
2018/04/14 全球购物
学校七一活动方案
2014/01/19 职场文书
中专生毕业个人鉴定
2014/02/26 职场文书
2015个人简历自我评价语
2015/03/11 职场文书
证券公司客户经理岗位职责
2015/04/09 职场文书
《检阅》教学反思
2016/02/22 职场文书
品牌形象定位,全面分析
2019/07/23 职场文书
Python使用sql语句对mysql数据库多条件模糊查询的思路详解
2021/04/12 Python
python高温预警数据获取实例
2022/07/23 Python