亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

比f(wàn)ile_get_contents穩(wěn)定的curl_get_contents分享

 更新時(shí)間:2012年01月11日 21:47:21   作者:  
相信使用過(guò)file_get_contents函數(shù)的朋友都知道,當(dāng)獲取的$url訪問(wèn)不了時(shí),會(huì)導(dǎo)致頁(yè)面漫長(zhǎng)的等待,甚至還能導(dǎo)致PHP進(jìn)程占用CPU達(dá)100%,因此這個(gè)函數(shù)就誕生了
分享一個(gè)實(shí)際在用的函數(shù):
復(fù)制代碼 代碼如下:

/*比f(wàn)ile_get_contents穩(wěn)定的多!$timeout為超時(shí)時(shí)間,單位是秒,默認(rèn)為1s。*/
function curl_get_contents($url,$timeout=1) {
$curlHandle = curl_init();
curl_setopt( $curlHandle , CURLOPT_URL, $url );
curl_setopt( $curlHandle , CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $curlHandle , CURLOPT_TIMEOUT, $timeout );
$result = curl_exec( $curlHandle );
curl_close( $curlHandle );
return $result;
}
$hx = curl_get_contents('http://chabaoo.cn');

相信使用過(guò)file_get_contents函數(shù)的朋友都知道,當(dāng)獲取的$url訪問(wèn)不了時(shí),會(huì)導(dǎo)致頁(yè)面漫長(zhǎng)的等待,甚至還能導(dǎo)致PHP進(jìn)程占用CPU達(dá)100%,因此這個(gè)函數(shù)就誕生了。curl的一些常識(shí)介紹
保留原file_get_contents函數(shù)的原因是當(dāng)讀取本地文件時(shí),用原生的file_get_contents顯然更合適。
另來(lái)自張宴的file_get_contnets的優(yōu)化,具體可看:http://chabaoo.cn/article/28030.htm
同樣是設(shè)置超時(shí)時(shí)間來(lái)解決這個(gè)問(wèn)題。如果沒(méi)裝curl,就必須得用這個(gè)方式了。
復(fù)制代碼 代碼如下:

$ctx = stream_context_create(array(
'http' => array(
'timeout' => 1 //設(shè)置一個(gè)超時(shí)時(shí)間,單位為秒
)
)
);
file_get_contents("http://chabaoo.cn/", 0, $ctx);

另外,據(jù)不完全測(cè)試,使用curl獲取頁(yè)面比用file_get_contents穩(wěn)定的多。

相關(guān)文章

最新評(píng)論