這
stream_context_create()函數(shù)是用來 創(chuàng)建打開文件的上下文件選項 ,用于fopen(),file_get_contents()等過程的超時設(shè)置、代理服務器、請求方式、頭信息設(shè)置的特殊過程。
比如說,上篇php教程中g(shù)d庫實現(xiàn)下載網(wǎng)頁所有圖片中,第10行:
利用了stream_context_create()設(shè)置代理服務器:
代碼如下:
//設(shè)置代理服務器
$opts = array('http'=>array('request_fulluri'=>true));
$context = stream_context_create($opts);
$content = file_get_contents($url,false,$context);
利用了stream_context_create()設(shè)置超時時間:
代碼如下:
$opts = array(
'http'=>array(
'method'=>"GET",
'timeout'=>60,
)
);
$context = stream_context_create($opts);
$html =file_get_contents('http://www.jb51.net', false, $context);
新聞熱點
疑難解答
圖片精選