执行、获取远程代码返回:file_get_contents 超时处理

来源:zhoz

天气终于晴了,但问题来了。在实现两个站点间用户数据同步,当使用php函数 file_get_contents抓取执行远程页面时,如果连接超时将会输出一个Fatal Error或相当的慢,结果导致下面的代码不能运行。先了解一下PHP file_get_contents() 函数
定义和用法
file_get_contents() 函数把整个文件读入一个字符串中。
和 file() 一样,不同的是 file_get_contents() 把文件读入一个字符串。
file_get_contents() 函数是用于将文件的内容读入到一个字符串中的首选方法。如果操作系统支持,还会使用内存映射技术来增强性能。
语法

file_get_contents(path,include_path,context,start,max_length)参数 描述
path 必需。规定要读取的文件。
include_path 可选。如果也想在 include_path 中搜寻文件的话,可以将该参数设为 "1"。
context 可选。规定文件句柄的环境。
context 是一套可以修改流的行为的选项。若使用 null,则忽略。
start 可选。规定在文件中开始读取的位置。该参数是 PHP 5.1 新加的。
max_length 可选。规定读取的字节数。该参数是 PHP 5.1 新加的。
说明
对 context 的支持是 PHP 5.0.0 添加的。
针对超时或页面过慢,一般可采取两个解决方案:
1. 利用file_get_contents()第三个参数

  1. <?php     
  2. $url = "http://zhoz.com/zhoz.php";     
  3. $ctx = stream_context_create(array(     
  4. ‘http’ => array(‘timeout’ => 10)     
  5.     )     
  6.     );     
  7. $result = @file_get_contents($url, 0, $ctx);     
  8. if($result){     
  9.         var_dump($result);     
  10.     }else{     
  11. echo " Buffer is empty";     
  12.     }     
  13. ?>  

 

「2009/01/22补记」:
此方法1,我经测试在本地反映良好,但如果在外网测试(环境:中国→美国服务器间)基本都是超时的情况。
测试了TimeOut基本没有用了,建议以下方式

2. 使用curl扩展库

  1. <?php     
  2. $url = "http://zhoz.com/zhoz.php";     
  3.   try {     
  4. echo date(‘Y-m-d h:i:s’);     
  5. echo "";     
  6. //$buffer = file_get_contents($url);   
  7. $buffer = zhoz_get_contents($url);     
  8. echo date(‘Y-m-d h:i:s’);     
  9. if(emptyempty($buffer)) {     
  10. echo " Buffer is empty";     
  11.         } else {     
  12. echo " Buffer is not empty";     
  13.         }     
  14.     } catch(Exception $e) {     
  15. echo "error ";     
  16.     }     
  17. function zhoz_get_contents($url, $second = 5) {     
  18. $ch = curl_init();     
  19.         curl_setopt($ch,CURLOPT_URL,$url);     
  20.         curl_setopt($ch,CURLOPT_HEADER,0);     
  21.         curl_setopt($ch,CURLOPT_TIMEOUT,$second);     
  22.         curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);     
  23. $content = curl_exec($ch);     
  24.         curl_close($ch);     
  25. return $content;     
  26.     }     
  27. ?>   

综述,根据系统环境来选择到底应用哪种方法:

  1. <?php  
  2. function vita_get_url_content($url) {  
  3. if(function_exists(‘file_get_contents’)) {  
  4. $file_contents = file_get_contents($url);  
  5. } else {  
  6. $ch = curl_init();  
  7. $timeout = 5;  
  8. curl_setopt ($ch, CURLOPT_URL, $url);  
  9. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);  
  10. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);  
  11. $file_contents = curl_exec($ch);  
  12. curl_close($ch);  
  13. }  
  14. return $file_contents;  
  15. }  
  16. ?> 

MySQL 5.1参考手册



评论权限被关闭.



赞助商

文章索引模板

好友推荐链接

强力推荐链接

分类目录

   

统计信息

Translator

Chinese (Simplified) flagItalian flagKorean flagChinese (Traditional) flagPortuguese flagEnglish flagGerman flagFrench flagSpanish flagJapanese flagArabic flagRussian flagGreek flagDutch flagBulgarian flagCzech flag
Croatian flagDanish flagFinnish flagPolish flagSwedish flagNorwegian flag          

标签

专利战 世界 中国 为什么 介绍 使用 公司 分析 利用 功能 原谅我红尘颠倒 发现 天涯 如何 实现 工具 慕容雪村 技术 插件 搜索引擎 支持 数据库 文件 方式 时间 服务器 用户 简单 系统 网站 美国 解决 谁的心不曾柔软 进行 部分 问题 AJAX blog Google LAN Linux MySQL PHP plugin WordPress

热门浏览