您当前的位置: 首页 > 网站编程 > PHP教程 > 执行、获取远程代码返回:file_get_contents 超时处理的问题详解

执行、获取远程代码返回:file_get_contents 超时处理的问题详解

作者:不详 来源:网络 发布时间: 2014-08-19 09:08 点击:
天气终于晴了,但问题来了。在实现两个站点间用户数据同步,当使用php函数 file_get_contents抓取执行远程页面时,如果连接超时将会输出一个Fatal Error或相当的慢,结果导致下面的代码不能运行。先了解一下PHP file_get_contents() 函数 定义和用法 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 添加的。

  针对超时或页面过慢,一般可采取两个解决方案:

  

  一. 利用file_get_contents()第三个参数

  


  复制代码 代码如下:

  $url = "http://zhoz.com/zhoz.php";

  $ctx = stream_context_create(array(

  ‘http' => array(‘timeout' => 10)

  )

  );

  $result = @file_get_contents($url, 0, $ctx);

  if($result){

  var_dump($result);

  }else{

  echo " Buffer is empty";

  }

  ?>

  此方法1,我经测试在本地反映良好,但如果在外网测试(环境:中国→美国服务器间)基本都是超时的情况。

  测试了TimeOut基本没有用了,建议以下方式

  

  二. 使用curl扩展库

  


  复制代码 代码如下:

  $url = "http://zhoz.com/zhoz.php";

  try {

  echo date(‘Y-m-d h:i:s');

  echo "";

  //$buffer = file_get_contents($url);

  $buffer = zhoz_get_contents($url);

  echo date(‘Y-m-d h:i:s');

  if(emptyempty($buffer)) {

  echo " Buffer is empty";

  } else {

  echo " Buffer is not empty";

  }

  } catch(Exception $e) {

  echo "error ";

  }

  function zhoz_get_contents($url, $second = 5) {

  $ch = curl_init();

  curl_setopt($ch,CURLOPT_URL,$url);

  curl_setopt($ch,CURLOPT_HEADER,0);

  curl_setopt($ch,CURLOPT_TIMEOUT,$second);

  curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);

  $content = curl_exec($ch);

  curl_close($ch);

  return $content;

  }

  ?>

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

  复制代码 代码如下:

  function vita_get_url_content($url) {

  if(function_exists(‘file_get_contents')) {

  $file_contents = file_get_contents($url);

  } else {

  $ch = curl_init();

  $timeout = 5;

  curl_setopt ($ch, CURLOPT_URL, $url);

  curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

  curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

  $file_contents = curl_exec($ch);

  curl_close($ch);

  }

  return $file_contents;

  }

  ?>
分享到:
本文"执行、获取远程代码返回:file_get_contents 超时处理的问题详解"由远航站长收集整理而来,仅供大家学习与参考使用。更多网站制作教程尽在远航站长站。
顶一下
(0)
0%
踩一下
(0)
0%
[点击 次] [返回上一页] [打印]
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 密码: 验证码:
关于本站 - 联系我们 - 网站声明 - 友情连接- 网站地图 - 站点地图 - 返回顶部
Copyright © 2007-2013 www.yhzhan.com(远航站长). All Rights Reserved .
远航站长:为中小站长提供最佳的学习与交流平台,提供网页制作与网站编程等各类网站制作教程.
官方QQ:445490277 网站群:26680406 网站备案号:豫ICP备07500620号-4