執(zhí)行、獲取遠(yuǎn)程代碼返回:file_get_contents 超時處理的問題詳解
更新時間:2013年06月25日 16:43:14 作者:
本篇文章是對執(zhí)行、獲取遠(yuǎn)程代碼返回:file_get_contents 超時處理的問題進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
天氣終于晴了,但問題來了。在實現(xiàn)兩個站點間用戶數(shù)據(jù)同步,當(dāng)使用php函數(shù) file_get_contents抓取執(zhí)行遠(yuǎn)程頁面時,如果連接超時將會輸出一個Fatal Error或相當(dāng)?shù)穆?,結(jié)果導(dǎo)致下面的代碼不能運行。先了解一下PHP file_get_contents() 函數(shù)
定義和用法
file_get_contents() 函數(shù)把整個文件讀入一個字符串中。
和 file() 一樣,不同的是 file_get_contents() 把文件讀入一個字符串。
file_get_contents() 函數(shù)是用于將文件的內(nèi)容讀入到一個字符串中的首選方法。如果操作系統(tǒng)支持,還會使用內(nèi)存映射技術(shù)來增強(qiáng)性能。
語法
file_get_contents(path,include_path,context,start,max_length)參數(shù) 描述
path 必需。規(guī)定要讀取的文件。
include_path 可選。如果也想在 include_path 中搜尋文件的話,可以將該參數(shù)設(shè)為 "1"。
context 可選。規(guī)定文件句柄的環(huán)境。
context 是一套可以修改流的行為的選項。若使用 null,則忽略。
start 可選。規(guī)定在文件中開始讀取的位置。該參數(shù)是 PHP 5.1 新加的。
max_length 可選。規(guī)定讀取的字節(jié)數(shù)。該參數(shù)是 PHP 5.1 新加的。
說明
對 context 的支持是 PHP 5.0.0 添加的。
針對超時或頁面過慢,一般可采取兩個解決方案:
一. 利用file_get_contents()第三個參數(shù)
$url = "http://zhoz.com/zhoz.php";
$ctx = stream_context_create(array(
‘http' => array(‘timeout' => 10)
)
);
$result = @file_get_contents($url, 0, $ctx);
if($result){
var_dump($result);
}else{
echo " Buffer is empty";
}
?>
此方法1,我經(jīng)測試在本地反映良好,但如果在外網(wǎng)測試(環(huán)境:中國→美國服務(wù)器間)基本都是超時的情況。
測試了TimeOut基本沒有用了,建議以下方式
二. 使用curl擴(kuò)展庫
$url = "http://zhoz.com/zhoz.php";
try {
echo date(‘Y-m-d h:i:s');
echo "";
//$buffer = file_get_contents($url);
$buffer = zhoz_get_contents($url);
echo date(‘Y-m-d h:i:s');
if(emptyempty($buffer)) {
echo " Buffer is empty";
} else {
echo " Buffer is not empty";
}
} catch(Exception $e) {
echo "error ";
}
function zhoz_get_contents($url, $second = 5) {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_TIMEOUT,$second);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
?>
綜述,根據(jù)系統(tǒng)環(huán)境來選擇到底應(yīng)用哪種方法:
function vita_get_url_content($url) {
if(function_exists(‘file_get_contents')) {
$file_contents = file_get_contents($url);
} else {
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
}
return $file_contents;
}
?>
定義和用法
file_get_contents() 函數(shù)把整個文件讀入一個字符串中。
和 file() 一樣,不同的是 file_get_contents() 把文件讀入一個字符串。
file_get_contents() 函數(shù)是用于將文件的內(nèi)容讀入到一個字符串中的首選方法。如果操作系統(tǒng)支持,還會使用內(nèi)存映射技術(shù)來增強(qiáng)性能。
語法
file_get_contents(path,include_path,context,start,max_length)參數(shù) 描述
path 必需。規(guī)定要讀取的文件。
include_path 可選。如果也想在 include_path 中搜尋文件的話,可以將該參數(shù)設(shè)為 "1"。
context 可選。規(guī)定文件句柄的環(huán)境。
context 是一套可以修改流的行為的選項。若使用 null,則忽略。
start 可選。規(guī)定在文件中開始讀取的位置。該參數(shù)是 PHP 5.1 新加的。
max_length 可選。規(guī)定讀取的字節(jié)數(shù)。該參數(shù)是 PHP 5.1 新加的。
說明
對 context 的支持是 PHP 5.0.0 添加的。
針對超時或頁面過慢,一般可采取兩個解決方案:
一. 利用file_get_contents()第三個參數(shù)
復(fù)制代碼 代碼如下:
$url = "http://zhoz.com/zhoz.php";
$ctx = stream_context_create(array(
‘http' => array(‘timeout' => 10)
)
);
$result = @file_get_contents($url, 0, $ctx);
if($result){
var_dump($result);
}else{
echo " Buffer is empty";
}
?>
此方法1,我經(jīng)測試在本地反映良好,但如果在外網(wǎng)測試(環(huán)境:中國→美國服務(wù)器間)基本都是超時的情況。
測試了TimeOut基本沒有用了,建議以下方式
二. 使用curl擴(kuò)展庫
復(fù)制代碼 代碼如下:
$url = "http://zhoz.com/zhoz.php";
try {
echo date(‘Y-m-d h:i:s');
echo "";
//$buffer = file_get_contents($url);
$buffer = zhoz_get_contents($url);
echo date(‘Y-m-d h:i:s');
if(emptyempty($buffer)) {
echo " Buffer is empty";
} else {
echo " Buffer is not empty";
}
} catch(Exception $e) {
echo "error ";
}
function zhoz_get_contents($url, $second = 5) {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_TIMEOUT,$second);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
?>
綜述,根據(jù)系統(tǒng)環(huán)境來選擇到底應(yīng)用哪種方法:
復(fù)制代碼 代碼如下:
function vita_get_url_content($url) {
if(function_exists(‘file_get_contents')) {
$file_contents = file_get_contents($url);
} else {
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
}
return $file_contents;
}
?>
您可能感興趣的文章:
相關(guān)文章
php-fpm超時時間設(shè)置request_terminate_timeout資源問題分析
之前發(fā)現(xiàn)一個php配置之后關(guān)于返回500和502的問題,今天看到一個兄弟寫的非常不錯,記錄一下2019-09-09
CentOS7系統(tǒng)搭建LAMP及更新PHP版本操作詳解
這篇文章主要介紹了CentOS7系統(tǒng)搭建LAMP及更新PHP版本操作,總結(jié)分析了CentOS7系統(tǒng)搭建LAMP及更新PHP版本操作相關(guān)原理、步驟、操作命令與注意事項,需要的朋友可以參考下2020-03-03
php數(shù)據(jù)庫操作model類(使用__call方法)
這篇文章主要介紹了php數(shù)據(jù)庫操作model類,使用__call方法實現(xiàn)了數(shù)據(jù)的查詢功能,需要的朋友可以參考下2016-11-11
php遞歸實現(xiàn)無限分類生成下拉列表的函數(shù)
php自定義函數(shù)之遞歸實現(xiàn)無限分類生成下拉列表,這樣可以提高效率,不用每次都從數(shù)據(jù)庫讀取數(shù)據(jù)。2010-08-08

