Function name: stream_context_get_params()
Applicable version: PHP 4 >= 4.3.0, PHP 5, PHP 7
Function description: stream_context_get_params() function gets parameters of the specified context.
Usage: stream_context_get_params(resource $stream_or_context): array|false
parameter:
Return value:
Example:
// 创建一个上下文$opts = array( 'http' => array( 'method' => 'GET', 'header' => 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3', ) ); $context = stream_context_create($opts); // 获取上下文的参数$params = stream_context_get_params($context); // 打印参数var_dump($params);
Output:
array(1) { ["http"]=> array(2) { ["method"]=> string(3) "GET" ["header"]=> string(139) "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3" } }
In the above example, we first create a context object $context
and set an HTTP request header. We then use the stream_context_get_params()
function to get the parameters of that context and store the result in the variable $params
. Finally, we use var_dump()
to print out the contents of this parameter. The output results show that $params
is an associative array that contains the HTTP request method and header information we set.