Function name: stream_context_set_default()
Applicable version: PHP 4 >= 4.3.0, PHP 5, PHP 7
Function Description: The stream_context_set_default() function sets the specified stream context to the default context. It sets the specified stream context as the default context, so that when opening files and URLs, the default context is used if the context is not explicitly specified.
Syntax: bool stream_context_set_default ( resource $context )
parameter:
Return value: Return true if the default context is successfully set; otherwise return false.
Example:
[ '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', ], ]); // Set the context to the default context if (stream_context_set_default($context)) { echo "Default context setting succeeds!"; } else { echo "Default context setting failed!"; } // Open URL $file = file_get_contents('https://www.example.com'); echo $file; ?>In the example above, a context resource containing the HTTP request header information is first created. Then, use the stream_context_set_default() function to set the context as the default context. Finally, a URL is opened using the file_get_contents() function and its contents are output to the screen. Because the default context is set, the file_get_contents() function will automatically use the context when the URL is opened.