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 相关文章推荐
多数据表共用一个页的新闻发布
Oct 09 PHP
介绍php设计模式中的工厂模式
Jun 12 PHP
linux下删除7天前日志的代码(php+shell)
Jan 02 PHP
openPNE常用方法分享
Nov 29 PHP
学习php过程中的一些注意点的总结
Oct 25 PHP
学习php设计模式 php实现原型模式(prototype)
Dec 07 PHP
两种php实现图片上传的方法
Jan 22 PHP
基于jQueryUI和Corethink实现百度的搜索提示功能
Nov 09 PHP
php 生成加密公钥加密私钥实例详解
Jun 16 PHP
彻底搞懂PHP 变量结构体
Oct 11 PHP
php语言注释,单行注释和多行注释
Jan 21 PHP
php实现统计IP数及在线人数的示例代码
Jul 22 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 SQL之where语句生成器
2009/03/24 PHP
PHP学习 变量使用总结
2011/03/24 PHP
php获取qq用户昵称和在线状态(实例分析)
2013/10/27 PHP
php判断类是否存在函数class_exists用法分析
2014/11/14 PHP
PHP中文编码小技巧
2014/12/25 PHP
js 未结束的字符串常量错误解决方法
2010/06/13 Javascript
setTimeout的延时为0时多个浏览器的区别
2012/05/23 Javascript
json原理分析及实例介绍
2012/11/29 Javascript
js修改table中Td的值(定义td的双击事件)
2013/01/10 Javascript
ECMAScript6函数默认参数
2015/06/12 Javascript
javascript html5移动端轻松实现文件上传
2020/03/27 Javascript
jquery mobile 实现自定义confirm确认框效果的简单实例
2016/06/17 Javascript
AngularJS中transclude用法详解
2016/11/03 Javascript
微信小程序 自定义对话框实例详解
2017/01/20 Javascript
jQuery点击导航栏选中更换样式的实现代码
2017/01/23 Javascript
vue.js实现含搜索的多种复选框(附源码)
2017/03/23 Javascript
angular-cli修改端口号【angular2】
2017/04/19 Javascript
详解vue axios中文文档
2017/09/12 Javascript
使用RN Animated做一个“添加购物车”动画的方法
2018/09/12 Javascript
Smartour 让网页导览变得更简单(推荐)
2019/07/19 Javascript
JavaScript实现联动菜单特效
2020/01/07 Javascript
vue+animation实现翻页动画
2020/06/29 Javascript
python实现各进制转换的总结大全
2017/06/18 Python
Python生成指定数量的优惠码实操内容
2019/06/18 Python
Django shell调试models输出的SQL语句方法
2019/08/29 Python
Python序列化与反序列化pickle用法实例
2019/11/11 Python
使用批处理脚本自动生成并上传NuGet包(操作方法)
2019/11/19 Python
python pyenv多版本管理工具的使用
2019/12/23 Python
python db类用法说明
2020/07/07 Python
红色康乃馨酒店:Red Carnation Hotels
2017/06/22 全球购物
文化宣传方案
2014/03/13 职场文书
宿舍标语大全
2014/06/19 职场文书
考试作弊检讨书1000字(5篇)
2014/10/19 职场文书
给老婆的保证书
2015/01/16 职场文书
教师节大会主持词
2015/07/06 职场文书
nginx 多个location转发任意请求或访问静态资源文件的实现
2021/03/31 Servers