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

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

天气终于晴了,但问题来了。在实现两个站点间用户数据同步,当使用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. $url = "http://zhoz.com/zhoz.php";     
  2. $ctx = stream_context_create(array(     
  3. ‘http’ => array(‘timeout’ => 10)     
  4.     )     
  5.     );     
  6. $result = @file_get_contents($url, 0, $ctx);     
  7. if($result){     
  8.         var_dump($result);     
  9.     }else{     
  10. echo " Buffer is empty";     
  11.     }     
  12. ?>  

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

2. 使用curl扩展库

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

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

  1. function vita_get_url_content($url) {  
  2. if(function_exists(‘file_get_contents’)) {  
  3. $file_contents = file_get_contents($url);  
  4. } else {  
  5. $ch = curl_init();  
  6. $timeout = 5;  
  7. curl_setopt ($ch, CURLOPT_URL, $url);  
  8. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);  
  9. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);  
  10. $file_contents = curl_exec($ch);  
  11. curl_close($ch);  
  12. }  
  13. return $file_contents;  
  14. }  
  15. ?> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值