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网上调查系统
Oct 09 PHP
php实现的简单压缩英文字符串的代码
Apr 24 PHP
PHP5 字符串处理函数大全
Mar 23 PHP
PHP实现多条件查询实例代码
Jul 17 PHP
linux下删除7天前日志的代码(php+shell)
Jan 02 PHP
PHP中MVC模式的模板引擎开发经验分享
Mar 23 PHP
PHP中文件读、写、删的操作(PHP中对文件和目录操作)
Mar 06 PHP
基于PHP常用函数的用法详解
May 10 PHP
PHP抓屏函数实现屏幕快照代码分享
Jan 02 PHP
php获得网站访问统计信息类Compete API用法实例
Apr 02 PHP
分享PHP守护进程类
Dec 30 PHP
PHP如何使用array_unshift()在数组开头插入元素
Sep 01 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常量详细解析
2015/10/27 PHP
浅谈Coreseek、Sphinx-for-chinaese、Sphinx+Scws的区别
2016/12/15 PHP
PHP实现的折半查询算法示例
2017/10/09 PHP
php获取小程序码的实现代码(B类接口)
2020/06/13 PHP
javascript[js]获取url参数的代码
2007/10/17 Javascript
javascript 文档的编码问题解决
2009/03/01 Javascript
javascript 类定义的4种方法
2009/09/12 Javascript
JQuery AJAX提交中文乱码的解决方案
2010/07/02 Javascript
Javascript实现Array和String互转换的方法
2015/12/21 Javascript
浅谈JavaScript 浏览器对象
2016/06/03 Javascript
javascript运算符语法全面概述
2016/07/14 Javascript
原生JS实现图片翻书效果
2017/02/16 Javascript
干货!教大家如何选择Vue和React
2017/03/13 Javascript
关于Vue背景图打包之后访问路径错误问题的解决
2017/11/03 Javascript
Vue仿支付宝支付功能
2018/05/25 Javascript
vue router返回到指定的路由的场景分析
2020/11/10 Javascript
js数组的基本使用总结
2021/01/18 Javascript
python正则表达式之作业计算器
2016/03/18 Python
Python判断文本中消息重复次数的方法
2016/04/27 Python
解决Python中字符串和数字拼接报错的方法
2016/10/23 Python
Python使用Windows API创建窗口示例【基于win32gui模块】
2018/05/09 Python
python面向对象多线程爬虫爬取搜狐页面的实例代码
2018/05/31 Python
通过python将大量文件按修改时间分类的方法
2018/10/17 Python
pytorch 调整某一维度数据顺序的方法
2018/12/08 Python
Python实现的简单线性回归算法实例分析
2018/12/26 Python
获取django框架orm query执行的sql语句实现方法分析
2019/06/20 Python
什么是python的自省
2020/06/21 Python
Python获取android设备cpu和内存占用情况
2020/11/15 Python
CSS3制作圆形滚动进度条动画的示例
2020/11/05 HTML / CSS
理工大学毕业生自荐信
2013/11/01 职场文书
法学自荐信
2014/06/20 职场文书
机电一体化应届生求职信
2014/08/09 职场文书
关心下一代工作先进事迹
2014/08/15 职场文书
教您怎么制定西餐厅运营方案 ?
2019/07/05 职场文书
Vue自定义铃声提示音组件的实现
2022/01/22 Vue.js
Python实现数据的序列化操作详解
2022/07/07 Python