stream_context_set_params
ストリーム/ラッパー/コンテキストのパラメーターを設定します
関数名:stream_context_set_params()
適用バージョン:PHP 5> = 5.3.0、PHP 7
関数の説明:stream_context_set_params()関数は、コンテキストパラメーターを設定するために使用されます。
構文:bool stream_context_set_params(リソース$ stream_or_context、array $ params)
パラメーター:
返品値:成功の真のリターン、および失敗に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 "设置上下文参数失败!"; }
上記の例では、最初に、strame_context_create()を使用してコンテキストリソースが作成され、一部のHTTP要求パラメーターがコンテキストで設定されます。次に、stream_context_set_params()を使用して、追加のパラメーター「通知」を設定します。最後に、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以上でのみ使用できます。