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批量删除数据
Jan 18 PHP
php将数据库中的电话号码读取出来并生成图片
Aug 31 PHP
献给php初学者(入门学习经验谈)
Oct 12 PHP
PHP UTF8中文字符截断函数代码
Sep 11 PHP
ThinkPHP3.1新特性之字段合法性检测详解
Jun 19 PHP
PHP使用http_build_query()构造URL字符串的方法
Apr 02 PHP
PHP实现验证码校验功能
Nov 16 PHP
php提取微信账单的有效信息
Oct 01 PHP
PHP常用正则表达式精选(推荐)
May 28 PHP
PHP rmdir()函数的用法总结
Jul 02 PHP
使用laravel和ajax实现整个页面无刷新的操作方法
Oct 03 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
晶体管单管来复再生式收音机
2021/03/02 无线电
PHP 超链接 抓取实现代码
2009/06/29 PHP
php之curl设置超时实例
2014/11/03 PHP
css把超出的部分显示为省略号的方法兼容火狐
2008/07/23 Javascript
JavaScript 笔记二 Array和Date对象方法
2010/05/22 Javascript
使用javascript实现监控视频播放并打印日志
2015/01/05 Javascript
jquery模拟进度条实现方法
2015/08/03 Javascript
JS+JSP通过img标签调用实现静态页面访问次数统计的方法
2015/12/14 Javascript
学习使用bootstrap3栅格系统
2016/04/12 Javascript
详解jQuery的表单验证插件--Validation
2016/12/21 Javascript
jQuery中的deferred使用方法
2017/03/27 jQuery
JS匹配日期和时间的正则表达式示例
2017/05/12 Javascript
node.js调用Chrome浏览器打开链接地址的方法
2017/05/17 Javascript
Express+Nodejs 下的登录拦截实现代码
2017/07/01 NodeJs
easyui datagrid 表格中操作栏 按钮图标不显示的解决方法
2017/07/27 Javascript
js正则表达式校验指定字符串的方法
2018/07/23 Javascript
浅析Vue.js 中的条件渲染指令
2018/11/19 Javascript
js回文数的4种判断方法示例
2019/06/04 Javascript
js实现点击图片在屏幕中间弹出放大效果
2019/09/11 Javascript
vue实现页面内容禁止选中功能,仅输入框和文本域可选
2019/11/09 Javascript
webpack+vue-cil 中proxyTable配置接口地址代理操作
2020/07/18 Javascript
[02:25]专访DOTA2负责人Erik 国际邀请赛暂不会离开西雅
2014/07/21 DOTA
Python使用函数默认值实现函数静态变量的方法
2014/08/18 Python
Python中使用PIPE操作Linux管道
2015/02/04 Python
django批量导入xml数据
2016/10/16 Python
python实现读取大文件并逐行写入另外一个文件
2018/04/19 Python
Python实现石头剪刀布游戏
2021/01/20 Python
HTML5中的进度条progress元素简介及兼容性处理
2016/06/02 HTML / CSS
Crocs卡骆驰洞洞鞋日本官方网站:Crocs日本
2016/08/25 全球购物
Expedia泰国:预订机票、酒店和旅游包(航班+酒店)
2016/09/27 全球购物
Mytheresa美国官网:德国知名的女性奢侈品电商
2017/05/27 全球购物
JSF界面控制层技术
2013/06/17 面试题
个人自荐材料
2014/05/23 职场文书
保洁公司服务承诺书
2014/05/28 职场文书
巾帼文明岗汇报材料
2014/12/24 职场文书
《艾尔登法环》1.03.3补丁上线 碎星伤害调整
2022/04/07 其他游戏