V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
chuxuezhe
V2EX  ›  问与答

file_get_contents 设置 timeout 失效?

  •  
  •   chuxuezhe · Jul 6, 2015 · 2727 views
    This topic created in 3956 days ago, the information mentioned may be changed or developed.

    我在使用file_get_contents进行获取内容的时候设置了超时时间3秒,但是在获取过程中,发现,有些请求远远超过这个值,代码如下:

    $opts = array(
    'http' => array(
    'method' => "GET",
    'timeout' => 3,
    )
    );

    $context = stream_context_create($opts);
    $t1 = time();
    @$file_data = file_get_contents($url, FALSE, $context);
    echo '|耗时:' . (time() - $t1) . '秒|';

    3 replies    2015-07-08 19:11:52 +08:00
    feiyuanqiu
        1
    feiyuanqiu  
       Jul 6, 2015
    我记得好像是发送三秒接收三秒总共有6秒超时的样子...好久以前遇到的问题了,最后还是改成了curl来处理
    b821025551b
        2
    b821025551b  
       Jul 6, 2015
    改成curl吧,file_get_content在某些url无法访问的情况下设置超时也没什么用。
    chuxuezhe
        3
    chuxuezhe  
    OP
       Jul 8, 2015
    谢谢,curl是可以生效的
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1153 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 17:11 · PVG 01:11 · LAX 10:11 · JFK 13:11
    ♥ Do have faith in what you're doing.