Function name: stream_context_set_params()
Applicable version: PHP 5 >= 5.3.0, PHP 7
Function description: The stream_context_set_params() function is used to set context parameters.
Syntax: bool stream_context_set_params ( resource $stream_or_context , array $params )
parameter:
Return value: Return true on success, and false on failure.
Example:
$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 "设置上下文参数失败!"; }
In the above example, first, a context resource is created using stream_context_create() and some HTTP request parameters are set in the context. Then use stream_context_set_params() to set an additional parameter 'notification'. Finally, use the file_get_contents() function to send the request and get the response.
$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 "设置上下文参数失败!"; }
In the above example, first, an empty context resource is created using stream_context_create(). Then use stream_context_set_params() to set an SSL-related parameter. Finally, use the file_get_contents() function to send an HTTPS request and get the response.
Note: The stream_context_set_params() function can only be used in PHP 5.3.0 and above.