使用file_get_contents远程调用一个纯文本总是超时怎么办? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
bobopu
V2EX    PHP

使用file_get_contents远程调用一个纯文本总是超时怎么办?

  •  
  •   bobopu 2013-08-26 10:43:19 +08:00 6369 次点击
    这是一个创建于 4430 天前的主题,其中的信息可能已经有所发展或是发生改变。
    RT,使用file_get_contents('http://xxxx.com/xxxx.txt');刚开始还可以,过上一阵就总是超时调用不了了,这该怎么办?
    16 条回复    1970-01-01 08:00:00 +08:00
    BOYPT
        1
    BOYPT  
       2013-08-26 10:46:49 +08:00
    配置一个超时参数:

    <?php
    $ctx = stream_context_create(array(
    'http' => array(
    'timeout' => 1
    )
    )
    );
    file_get_contents("http://example.com/", 0, $ctx);
    ?>


    外部资源属于不可控因素,程序里面要做好这类异常处理。
    bobopu
        2
    bobopu  
    OP
       2013-08-26 10:57:53 +08:00
    @BOYPT 请问当大于设定的时间时是不显示,还是输出错误代码呢?
    anewg
        3
    anewg  
       2013-08-26 11:29:22 +08:00
    @bobopu file_get_contents返回false
    lusin
        4
    lusin  
       2013-08-26 11:34:15 +08:00
    用curl好像会好一些
    bobopu
        5
    bobopu  
    OP
       2013-08-26 11:36:44 +08:00   1
    @BOYPT
    @anewg 提示
    Warning: file_get_contents(http://xxxx.com/xxxx.txt) [function.file-get-contents]: failed to open stream: Connection timed out in /home/xxx/xxx on line 8
    BOYPT
        6
    BOYPT  
       2013-08-26 11:38:15 +08:00
    @bobopu 这要你自己决定啊,超时之后是自动重试、显示给用户、还是换一个地址,你都知道这一步了下一步自然自己决定了。
    hennywei
        7
    hennywei  
       2013-08-26 11:57:17 +08:00 div class="sep5">
    建议用curl
    itaotao
        8
    itaotao  
       2013-08-26 12:20:10 +08:00
    set_time_limit(0)
    anewg
        9
    anewg  
       2013-08-26 12:30:48 +08:00
    @bobopu 你连接的页面要存在才返回false,你在写个test.php,里面sleep(10)。file_get_contents("http://localhost/test.php", 0, $ctx)返回的就是false
    Sokos
        10
    Sokos  
       2013-08-26 12:43:20 +08:00
    尽量不用 file_get_contents。参考这文章 http://www.s135.com/file_get_contents
    vibbow
        11
    vibbow  
       2013-08-26 13:27:38 +08:00   1
    hello570111
        12
    hello570111  
       2013-08-26 16:13:27 +08:00
    file_get_contents默认没有超时时间,而且出错之后会终止整个程序而不是继续执行后面的语句,最多调试的时候用用,正式情况下最好用curl。
    bobopu
        13
    bobopu  
    OP
       2013-08-26 16:24:20 +08:00
    @anewg 哦哦,我要调用的是纯文本文件,咋弄?
    bobopu
        14
    bobopu  
    OP
       2013-08-26 16:29:52 +08:00
    @hennywei
    @hello570111
    @BOYPT 换做curl后又提示Fatal error: Call to undefined function curl() in /xxx/xxx.php on line 8 了。。。。
    hello570111
        15
    hello570111  
       2013-08-26 17:22:38 +08:00 via Android
    @bobopu 因为php没有curl()这个函数,它不是这么用的(吐槽一下,难怪大家都喜欢用file_get_contents,太方便了),需要先curl_init()再curl_setopt()设置什么的。搜索一下“php curl使用”就知道了,可以自己再封装一下。
    bobopu
        16
    bobopu  
    OP
       2013-08-26 17:32:33 +08:00
    @hello570111 好吧,我真是太菜了。。。。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2646 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 06:37 PVG 14:37 LAX 23:37 JFK 02:37
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86