(PHP 5 >= 5.1.0, PHP 7, PHP 8)
stream_context_get_default — 取得預設串流上下文
傳回預設的串流內容,當檔案操作 (例如 fopen()、file_get_contents() 等等) 沒有指定內容參數時,就會使用這個預設的串流內容。您可以使用與 stream_context_create() 相同的語法,透過這個函式設定預設串流內容的選項。
一個串流內容 資源。
版本 | 說明 |
---|---|
8.0.0 |
options 現在可以是 null。 |
範例 #1 使用 stream_context_get_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"
)
);
$alternate_opts = array(
'http'=>array(
'method'=>"POST",
'header'=>"Content-type: application/x-www-form-urlencoded\r\n" .
"Content-length: " . strlen("baz=bomb"),
'content'=>"baz=bomb"
)
);
$default = stream_context_get_default($default_opts);
$alternate = stream_context_create($alternate_opts);
/* 發送一個標準的 GET 請求到位於 10.54.1.39 的代理伺服器
* 使用 $default_opts 中指定的上下文選項來請求 www.example.com
*/
readfile('http://www.example.com');
/* 直接發送一個 POST 請求到 www.example.com
* 使用 $alternate_opts 中指定的上下文選項
*/
readfile('http://www.example.com', false, $alternate);
?>