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 相关文章推荐
web方式ftp
Oct 09 PHP
如何对PHP程序中的常见漏洞进行攻击(上)
Oct 09 PHP
php开发工具之vs2005图解
Jan 12 PHP
mysql 中InnoDB和MyISAM的区别分析小结
Apr 15 PHP
fleaphp crud操作之find函数的使用方法
Apr 23 PHP
PHP的基本常识小结
Jul 05 PHP
利用PHP将部分内容用星号替换
Apr 21 PHP
CI操作cookie的方法分析(基于helper类库)
Mar 28 PHP
基于PHP后台的Android新闻浏览客户端
May 23 PHP
PHP自定义多进制的方法
Nov 03 PHP
浅析PHP类的反射来实现依赖注入过程
Feb 06 PHP
PHP pthreads v3下同步处理synchronized用法示例
Feb 21 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/03/16 PHP
关于Zend Studio 配色方案插件的介绍
2013/06/24 PHP
PHP基于GD库的缩略图生成代码(支持jpg,gif,png格式)
2014/06/19 PHP
php通过分类列表产生分类树数组的方法
2015/04/20 PHP
PHP实现中文圆形印章特效
2015/06/19 PHP
php编程每天必学之表单验证
2016/03/01 PHP
PHP排序算法之基数排序(Radix Sort)实例详解
2018/04/21 PHP
javascript判断两个IP地址是否在同一个网段的实现思路
2013/12/13 Javascript
js实现的GridView即表头固定表体有滚动条且可滚动
2014/02/19 Javascript
setinterval()与clearInterval()JS函数的调用方法
2015/01/21 Javascript
javascript+canvas实现刮刮卡抽奖效果
2015/07/29 Javascript
IE中document.createElement的iframe无法设置属性name的解决方法
2015/09/14 Javascript
JS提交form表单实例分析
2015/12/10 Javascript
jQuery+css实现的tab切换标签(兼容各浏览器)
2016/01/28 Javascript
js日期插件dateHelp获取本月、三个月、今年的日期
2016/03/07 Javascript
基于jQuery实现弹出可关闭遮罩提示框实例代码
2016/07/18 Javascript
JS 数字转换为大写金额的简单实例
2016/08/04 Javascript
Angular.js之作用域scope'@','=','&amp;'实例详解
2017/02/28 Javascript
VueJs监听window.resize方法示例
2018/01/17 Javascript
JavaScript门道之标准库
2018/05/26 Javascript
js动态生成表格(节点操作)
2021/01/12 Javascript
[06:13]DOTA2进化论(修改版)
2013/10/08 DOTA
对Python中内置异常层次结构详解
2018/10/18 Python
获取django框架orm query执行的sql语句实现方法分析
2019/06/20 Python
python正则-re的用法详解
2019/07/28 Python
python3 mmh3安装及使用方法
2019/10/09 Python
PyQt5多线程刷新界面防假死示例
2019/12/13 Python
django修改models重建数据库的操作
2020/03/31 Python
浅谈pc和移动端的响应式的使用
2019/01/03 HTML / CSS
美国家用和厨房电器销售网站:Appliances Connection
2020/01/24 全球购物
学生处主任岗位职责
2013/12/01 职场文书
晚会主持词开场白
2014/03/17 职场文书
地质灾害防治方案
2014/05/14 职场文书
html+css实现文字折叠特效实例
2021/06/02 HTML / CSS
从零开始在Centos7上部署SpringBoot项目
2022/04/07 Servers
vue动态绑定style样式
2022/04/20 Vue.js