深入php函數(shù)file_get_contents超時(shí)處理的方法詳解
更新時(shí)間:2013年06月03日 16:45:36 作者:
本篇文章是對php函數(shù)file_get_contents超時(shí)處理的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
一.增加超時(shí)的時(shí)間限制
這里需要注意:set_time_limit只是設(shè)置你的PHP程序的超時(shí)時(shí)間,而不是file_get_contents函數(shù)讀取URL的超時(shí)時(shí)間。真正的修改 file_get_contents延時(shí)可以用resource $context的timeout參數(shù):
$opts = array(
'http'=>array(
'method'=>"GET",
'timeout'=>60,
) );
$context = stream_context_create($opts); $html =file_get_contents('http://www.example.com', false, $context);
二、一次有延時(shí)的話那就多試幾次
有時(shí)候失敗是因?yàn)榫W(wǎng)絡(luò)等因素造成,沒有解決辦法,但是可以修改程序,失敗時(shí)重試幾次,仍然失敗就放棄,因?yàn)閒ile_get_contents()如果失敗將返回 FALSE,所以可以下面這樣編寫代碼:
$cnt=0;
while($cnt < 3 && ($str=@file_get_contents('http...'))===FALSE) $cnt++;
以上方法對付超時(shí)已經(jīng)OK了。
有人發(fā)現(xiàn)了'method'=>”GET”,GET也可以設(shè)置成post,函數(shù)如下
function Post($url, $post = null)
{
$context = array();
if (is_array($post)) {
ksort($post);
$context['http'] = array (
'timeout'=>60,
'method' => 'POST',
'content' => http_build_query($post, '', '&'),
);
}
return file_get_contents($url, false, stream_context_create($context));
}
$data = array (
'name' => 'test',
'email' => 'test@gmail.com',
'submit' => 'submit',
);
echo Post('http://www.example.com', $data);
這里需要注意:set_time_limit只是設(shè)置你的PHP程序的超時(shí)時(shí)間,而不是file_get_contents函數(shù)讀取URL的超時(shí)時(shí)間。真正的修改 file_get_contents延時(shí)可以用resource $context的timeout參數(shù):
復(fù)制代碼 代碼如下:
$opts = array(
'http'=>array(
'method'=>"GET",
'timeout'=>60,
) );
$context = stream_context_create($opts); $html =file_get_contents('http://www.example.com', false, $context);
二、一次有延時(shí)的話那就多試幾次
有時(shí)候失敗是因?yàn)榫W(wǎng)絡(luò)等因素造成,沒有解決辦法,但是可以修改程序,失敗時(shí)重試幾次,仍然失敗就放棄,因?yàn)閒ile_get_contents()如果失敗將返回 FALSE,所以可以下面這樣編寫代碼:
$cnt=0;
while($cnt < 3 && ($str=@file_get_contents('http...'))===FALSE) $cnt++;
以上方法對付超時(shí)已經(jīng)OK了。
有人發(fā)現(xiàn)了'method'=>”GET”,GET也可以設(shè)置成post,函數(shù)如下
復(fù)制代碼 代碼如下:
function Post($url, $post = null)
{
$context = array();
if (is_array($post)) {
ksort($post);
$context['http'] = array (
'timeout'=>60,
'method' => 'POST',
'content' => http_build_query($post, '', '&'),
);
}
return file_get_contents($url, false, stream_context_create($context));
}
$data = array (
'name' => 'test',
'email' => 'test@gmail.com',
'submit' => 'submit',
);
echo Post('http://www.example.com', $data);
您可能感興趣的文章:
- PHP-CGI進(jìn)程CPU 100% 與 file_get_contents 函數(shù)的關(guān)系分析
- 解析PHP中的file_get_contents獲取遠(yuǎn)程頁面亂碼的問題
- file_get_contents("php://input", "r")實(shí)例介紹
- php讀取本地文件常用函數(shù)(fopen與file_get_contents)
- PHP file_get_contents設(shè)置超時(shí)處理方法
- php 使用file_get_contents讀取大文件的方法
- PHP使用fopen與file_get_contents讀取文件實(shí)例分享
相關(guān)文章
php+ajax 實(shí)現(xiàn)輸入讀取數(shù)據(jù)庫顯示匹配信息
這篇文章主要介紹了php+ajax 實(shí)現(xiàn)輸入讀取數(shù)據(jù)庫顯示匹配信息的相關(guān)資料,需要的朋友可以參考下2015-10-10
php下拉選項(xiàng)的批量操作的實(shí)現(xiàn)代碼
這篇文章介紹了php下拉選項(xiàng)的批量操作的實(shí)現(xiàn)代碼,有需要的朋友可以參考一下2013-10-10
PHP和.net中des加解密的實(shí)現(xiàn)方法
PHP和.net中des加解密的實(shí)現(xiàn)方法,需要的朋友可以參考一下2013-02-02
yii2中使用webuploader實(shí)現(xiàn)圖片上傳的實(shí)戰(zhàn)項(xiàng)目
本篇文章主要主要介紹了yii2中使用webuploader實(shí)現(xiàn)圖片上傳的實(shí)戰(zhàn)項(xiàng)目,具有一定的參考價(jià)值,有興趣的同學(xué)可以了解一下2017-09-09
phpcms手機(jī)內(nèi)容頁面添加上一篇和下一篇
本文給大家分享的是在phpcms中的手機(jī)內(nèi)容詳情頁里添加上一篇和下一篇的方法,十分的簡單實(shí)用,有需要的小伙伴可以參考下。2015-06-06
護(hù)衛(wèi)神php套件 php版本升級方法(php5.5.24)
這篇文章主要介紹了護(hù)衛(wèi)神php套件 php版本升級方法(php5.5.24),需要的朋友可以參考下2015-05-05

