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); ?> 外部资源属于不可控因素,程序里面要做好这类异常处理。 |
![]() | 4 lusin 2013-08-26 11:34:15 +08:00 用curl好像会好一些 |
![]() | 5 bobopu OP ![]() @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 |
7 hennywei 2013-08-26 11:57:17 +08:00 div class="sep5"> 建议用curl |
![]() | 8 itaotao 2013-08-26 12:20:10 +08:00 set_time_limit(0) |
![]() | 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 |
![]() | 10 Sokos 2013-08-26 12:43:20 +08:00 尽量不用 file_get_contents。参考这文章 http://www.s135.com/file_get_contents |
![]() | 11 vibbow 2013-08-26 13:27:38 +08:00 ![]() |
![]() | 12 hello570111 2013-08-26 16:13:27 +08:00 file_get_contents默认没有超时时间,而且出错之后会终止整个程序而不是继续执行后面的语句,最多调试的时候用用,正式情况下最好用curl。 |
![]() | 14 bobopu OP @hennywei @hello570111 @BOYPT 换做curl后又提示Fatal error: Call to undefined function curl() in /xxx/xxx.php on line 8 了。。。。 |
![]() | 15 hello570111 2013-08-26 17:22:38 +08:00 via Android @bobopu 因为php没有curl()这个函数,它不是这么用的(吐槽一下,难怪大家都喜欢用file_get_contents,太方便了),需要先curl_init()再curl_setopt()设置什么的。搜索一下“php curl使用”就知道了,可以自己再封装一下。 |
![]() | 16 bobopu OP @hello570111 好吧,我真是太菜了。。。。 |