stream_context_create
創建資源流上下文
函數名稱:stream_context_create()
函數功能:創建一個流上下文
適用版本:所有版本
函數用法: stream_context_create ( array $options = ? , array $params = ? ) : resource
參數說明:
options:一個關聯數組,用於設置流上下文的選項。可選參數包括:
params:一個關聯數組,用於設置流上下文的額外參數。可選參數包括:
返回值:成功時返回一個資源類型的流上下文,失敗時返回false。
示例代碼:
// 创建一个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
注意事項: