stream_context_create
Create resource flow context
Function name: stream_context_create()
Function function: Create a stream context
Applicable versions: All versions
Function usage: stream_context_create ( array $options = ? , array $params = ? ) : resource
Parameter description:
options: an associative array that sets the options for stream context. Optional parameters include:
params: an associative array that sets additional parameters for the stream context. Optional parameters include:
Return value: Returns a resource type stream context when successful, and returns false when failure.
Sample code:
// 创建一个HTTP请求的流上下文$options = array( 'http' => array( 'method' => 'POST', 'header' => 'Content-Type: application/json', 'content' => json_encode(array('name' => 'John')), ), ); $context = stream_context_create($options); // 发送HTTP请求$response = file_get_contents('http://example.com/api', false, $context); // 创建一个SSL连接的流上下文$options = array( 'ssl' => array( 'verify_peer' => true, 'cafile' => '/path/to/cert.pem', ), ); $context = stream_context_create($options); // 打开一个SSL连接$socket = stream_socket_client('ssl://example.com:443', $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context); if (!$socket) { die("Failed to connect: $errstr ($errno)"); } // 其他用法和示例请参考官方文档:https://www.php.net/manual/en/function.stream-context-create.php
Notes: