牛牛在线工具

stream_context_set_params()

收录 8868 个 PHP 函数,含用途说明与用法示例。

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 ) 参数: $stream_or_context:必需,一个资源流或者是一个已经创建的上下文资源。 $params:必需,一个关联数组,包含要设置的参数。 返回值:成功时返回 true,失败时返回 false。 示例: 使用 stream_context_create() 创建上下文资源,并使用 stream_context_set_params() 设置上下文参数: $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 及以上版本中使用。