哪你就用fsockopen,可以加时间限制

解决方案 »

  1.   

    if(!file_get_contents($url))
    {
         //...
    }
      

  2.   

    如果失败,file_get_contents() 将返回 FALSE
      

  3.   

    回:iasky(C#_ASP_PHP) 
    if(!file_get_contents($url))
    {
         //...
    }
    这种方法没有限定时间,行不通。
    讨论下这个代码行不行得通
    $begintime=time();
    while((time()-$begintime)<2)
    {
    $line=file_get_contents("http://localhost/phpinfo.php");
      if($line)
      {
      break;
      }
    }
    echo $line;
      

  4.   

    file_get_contents没有这个功能。
      

  5.   

    加前面加上
    set_time_limit(500);
      

  6.   

    回:reinforceli()
    试试把控制写为函数吧:
    function getContent($file){
    $begintime=time();
    while((time()-$begintime)<2)
    {
    $line=file_get_contents("$file");
    if($line)
    {
    return $line;
       }
    }
    }
    调用这个函数应该就可以了!
      

  7.   

    @fsockopen($host, $port,&$errno, &$errstr, $time_out);
    用这个函数进行操作,后边哪个time_out 可以让你控制
      

  8.   

    回 wuyz124(玖头鸟) resonru() 
    set_time_limit(500);
    你这个是设置整个网页的执行时间。 不行的。
      

  9.   

    用fsockopen我知道可以。
       但我不是很想用,如果真的没办法就只好用它了。
      

  10.   

    回zhys9(OoP.plorer) function getContent($file){
    $begintime=time();
    while((time()-$begintime)<2)
    {
    $line=file_get_contents("$file");
    if($line)
    {
    return $line;
       }
    }
    }
    调用这个函数应该就可以了!//////////////////////////////////
    加进函数还是一个样,没用。根本不能限制file_get_contents的时间。
    PHP是线性执行代码的,看来是解决不了这个问题的了。
      

  11.   

    fsockopen的最后个参数是可以控制时间的~例子可以去看看SMTP的类
      

  12.   

    还有我发现
    php程序在本地可以执行半个小时以后发现十分种都超时了
      

  13.   

    fsockopen的timeout参数是用于建立连接超时的,设置读取文件超时的话要用stream_set_timeout(),手册里面有例子$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
    if (!$fp) {
        echo "$errstr ($errno)<br />\n";
    } else {
        $out = "GET / HTTP/1.1\r\n";
        $out .= "Host: www.example.com\r\n";
        $out .= "Connection: Close\r\n\r\n";    fwrite($fp, $out);
        while (!feof($fp)) {
            echo fgets($fp, 128);
        }
        fclose($fp);
    }这个是基于socket的,不是文件系统
    本地的话读取的文件要放在web目录下,用localhost:80来连接
      

  14.   

    应该是这个例子
    $fp = fsockopen("www.example.com", 80);
    if (!$fp) {
        echo "Unable to open\n";
    } else {    fwrite($fp, "GET / HTTP/1.0\r\n\r\n");
        stream_set_timeout($fp, 2);
        $res = fread($fp, 2000);    $info = stream_get_meta_data($fp);
        fclose($fp);    if ($info['timed_out']) {
            echo 'Connection timed out!';
        } else {
            echo $res;
        }}
      

  15.   

    以前写的函数,直接用socket不好控制 /**
    * 读取$url页面,返回$str页面内容,调用f_fsock()函数,text
    */
    function f_fsock($website,$url)
    {
    $fp = fsockopen($website, 80, $errno, $errstr, 30);
    if ($fp)
    {
    $out = "GET " . $url . " HTTP/1.1\r\n";
    $out .= "Host: " . $website . "\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    $str = "";
    while (!feof($fp))
    {
    stream_set_timeout($fp, 60);
    $str .= fread($fp, 8192);
    $info = stream_get_meta_data($fp);
    if ($info['timed_out'])
    {
    $str = "";
    break;
    }
    }
    fclose($fp);
    return $str;
    }
    else
    {
    ;
    }
    }