stream_context_set_params
設置流/包裝器/上下文的參數
函數名稱:stream_context_set_params()
適用版本:PHP 5 >= 5.3.0, PHP 7
函數描述:stream_context_set_params() 函數用於設置上下文參數。
語法:bool stream_context_set_params ( resource $stream_or_context , array $params )
參數:
返回值:成功時返回true,失敗時返回false。
示例:
$opts = array( 'http' => array( 'method' => 'POST', 'header' => 'Content-Type: application/x-www-form-urlencoded', 'content' => http_build_query(array('key1' => 'value1', 'key2' => 'value2')) ) ); $context = stream_context_create($opts); $params = array( 'notification' => 'on' ); if (stream_context_set_params($context, $params)) { $file = file_get_contents('http://example.com', false, $context); echo $file; } else { echo "设置上下文参数失败!"; }
上述示例中,首先使用stream_context_create() 創建了一個上下文資源,並將一些HTTP 請求參數設置在上下文中。然後使用stream_context_set_params() 設置了一個額外的參數'notification'。最後,使用file_get_contents() 函數發送請求並獲取響應。
$context = stream_context_create(); $params = array( 'ssl' => array( 'verify_peer' => true, 'verify_peer_name' => true, 'allow_self_signed' => false ) ); if (stream_context_set_params($context, $params)) { $file = file_get_contents('https://example.com', false, $context); echo $file; } else { echo "设置上下文参数失败!"; }
上述示例中,首先使用stream_context_create() 創建了一個空的上下文資源。然後使用stream_context_set_params() 設置了一個SSL 相關的參數。最後,使用file_get_contents() 函數發送HTTPS 請求並獲取響應。
注意:stream_context_set_params() 函數只能在PHP 5.3.0 及以上版本中使用。