鍍金池/ 問答/PHP  HTML/ 關(guān)于php file_get_content超時設(shè)置的疑惑

關(guān)于php file_get_content超時設(shè)置的疑惑

我的一個程序會發(fā)起一個http請求到另一個項目,用的file_get_content函數(shù),我需要保證請求發(fā)出了,但是不需要返回結(jié)果,我可以設(shè)置file_get_content的超時時間為1秒嗎?因為目前設(shè)置的3秒,很多慢請求

回答
編輯回答
墨沫

file_get_contents()是專門用來獲取文件數(shù)據(jù)流的,要么拿到,要么報錯,你的目的如果只是想要請求,試試curl,可以設(shè)置請求時長

2017年5月10日 10:48
編輯回答
敢試

兩個辦法

1.直接臨時修改ini配置

ini_set('default_socket_timeout', 900);//單位秒

2.這個函數(shù)也是有其他參數(shù)的

$ctx = stream_context_create(array('http'=>
    array(
        'timeout' => 1200,//單位秒
    )
));

echo file_get_contents('http://example.com/', false, $ctx);
2018年8月31日 15:04