$url = 'http://www.xxx.com/xxx.txt';
$fp = fopen($url,'r');如果超过2秒没有打开刚返回false;可以实现吗?

解决方案 »

  1.   


    If you need fopen() on a URL to timeout, you can do like:
    <?php
      $timeout = 3;
      $old = ini_set('default_socket_timeout', $timeout);
      $file = fopen('http://example.com', 'r');
      ini_set('default_socket_timeout', $old);
      stream_set_timeout($file, $timeout);
      stream_set_blocking($file, 0);
      //the rest is standard
    ?> 
      

  2.   

    http://edlongren.blogbus.com/logs/41214237.html
      

  3.   

    上面的将对整个代码起作用,针对该连接的请看手册以下部分及相关链接http://ca2.php.net/manual/en/function.file-get-contents.php  (example #4)
    http://ca2.php.net/manual/en/function.stream-context-create.php
    http://ca2.php.net/manual/en/context.http.php