2024 PHP Conference Japan (日本 PHP 研討會)

stream_context_get_default

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

stream_context_get_default取得預設串流上下文

說明

stream_context_get_default(?陣列 $options = null): 資源

傳回預設的串流內容,當檔案操作 (例如 fopen()file_get_contents() 等等) 沒有指定內容參數時,就會使用這個預設的串流內容。您可以使用與 stream_context_create() 相同的語法,透過這個函式設定預設串流內容的選項。

參數

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

傳回值

一個串流內容 資源

更新日誌

版本 說明
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);

?>

參見

新增筆記

使用者貢獻的筆記

此頁面尚無使用者貢獻的筆記。
To Top