PHP Conference Japan 2024

stream_context_set_default

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

stream_context_set_default設定預設串流上下文

說明

stream_context_set_default(陣列 $options): 資源

設定預設串流上下文,當檔案操作 (例如 fopen()file_get_contents() 等等) 沒有指定上下文參數時,將使用此預設上下文。語法與 stream_context_create() 相同。

參數

options

要為預設上下文設定的選項。

注意事項:

options 必須是一個關聯陣列的關聯陣列,格式為 $arr['wrapper']['option'] = $value

返回值

返回預設的串流上下文。

範例

範例 #1 stream_context_set_default() 範例

<?php
$default_opts
= array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Cookie: foo=bar",
'proxy'=>"tcp://10.54.1.39:8000"
)
);

$default = stream_context_set_default($default_opts);

/* 使用 $default_opts 中指定的上下文選項,
* 向位於 10.54.1.39 的代理伺服器發送一個標準的 GET 請求,
* 取得 www.example.com 的內容。
*/
readfile('http://www.example.com');
?>

相關函式

新增筆記

使用者貢獻的筆記 1 則筆記

1
Mahmoud Saeed
3 年前
值得一提的是,傳遞一個空陣列並**不會**重置預設選項。

<?php
stream_context_set_default
(array()); // 無作用。
?>
To Top