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中的MVC模式运用技巧
May 03 PHP
sourcesafe管理phpproj文件的补充说明(downmoon)
Apr 11 PHP
PHP中用接口、抽象类、普通基类实现“面向接口编程”与“耦合方法”简述
Mar 23 PHP
php设计模式 Mediator (中介者模式)
Jun 26 PHP
Destoon旺旺无法正常显示,点击提示“会员名不存在”的解决办法
Jun 21 PHP
PHP基于CURL进行POST数据上传实例
Nov 10 PHP
19个Android常用工具类汇总
Dec 30 PHP
php去除html标记的原生函数详解
Jan 27 PHP
注释PHP和html混合代码的小技巧(分享)
Nov 03 PHP
php基于协程实现异步的方法分析
Jul 17 PHP
Laravel 手动开关 Eloquent 修改器的操作方法
Dec 30 PHP
laravel ajax curd 搜索登录判断功能的实现
Apr 17 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
全国FM电台频率大全 - 31 新疆维吾尔族自治区
2020/03/11 无线电
php 变量定义方法
2009/06/14 PHP
PHP判断图片格式的七种方法小结
2013/06/03 PHP
PHP中常用的魔术方法
2017/04/28 PHP
Yii2实现自定义独立验证器的方法
2017/05/05 PHP
ubutu 16.04环境下,PHP与mysql数据库,网页登录验证实例讲解
2017/07/20 PHP
PHP实现压缩图片尺寸并转为jpg格式的方法示例
2018/05/10 PHP
jQuery ctrl+Enter shift+Enter实现代码
2010/02/07 Javascript
理解JavaScript表单的基础知识
2016/01/25 Javascript
用jmSlip编写移动端顶部日历选择控件
2016/10/24 Javascript
JavaScript实现按键精灵的原理分析
2017/02/21 Javascript
jquery实现自定义图片裁剪功能【推荐】
2017/03/08 Javascript
javascript 玩转Date对象(实例讲解)
2017/07/11 Javascript
js案例之鼠标跟随jquery版(实例讲解)
2017/07/21 jQuery
你应该知道的几类npm依赖包管理详解
2017/10/06 Javascript
JS实现定时任务每隔N秒请求后台setInterval定时和ajax请求问题
2017/10/15 Javascript
如何用webpack4.0撸单页/多页脚手架 (jquery, react, vue, typescript)
2019/06/18 jQuery
vue+springboot图片上传和显示的示例代码
2020/02/14 Javascript
[01:48]完美圣典齐天大圣至宝宣传片
2016/12/17 DOTA
Python将图片批量从png格式转换至WebP格式
2020/08/22 Python
使用Anaconda3建立虚拟独立的python2.7环境方法
2018/06/11 Python
python控制nao机器人身体动作实例详解
2019/04/29 Python
python 通过可变参数计算n个数的乘积方法
2019/06/13 Python
python conda操作方法
2019/09/11 Python
Python 实现数组相减示例
2019/12/27 Python
Python如何定义接口和抽象类
2020/07/28 Python
HTML5 背景的显示区域实现
2020/07/09 HTML / CSS
Tory Burch德国官网:美国时尚生活品牌
2018/01/03 全球购物
在线课程:Skillshare
2019/04/02 全球购物
公司财务自我评价分享
2013/12/17 职场文书
《金钱的魔力》教学反思
2014/02/24 职场文书
新闻报道策划方案
2014/06/11 职场文书
2014年学生资助工作总结
2014/12/18 职场文书
同学聚会祝酒词
2015/08/10 职场文书
导游词之青岛崂山
2019/12/27 职场文书
JavaScript实现班级抽签小程序
2021/05/19 Javascript