Home > Article > Backend Development > Usage example of PHP stream_context_create() function_PHP tutorial
This article mainly introduces examples of using the PHP stream_context_create() function. The stream_context_create() function is used to create the context of an open file. File options are used for timeout settings, proxy servers, request methods, and header information settings for fopen(), file_get_contents() and other processes. Friends in need can refer to the following
The stream_context_create() function is used to create context options for opening files, and is a special process used for timeout settings, proxy servers, request methods, and header information settings for fopen(), file_get_contents() and other processes.
For example, in the last PHP tutorial, the gd library implements downloading of all pictures on the web page, line 10:
Use stream_context_create() to set the proxy server:
The code is as follows:
//Set proxy server
$opts = array('http'=>array('request_fulluri'=>true));
$context = stream_context_create($opts);
$content = file_get_contents($url,false,$context);
Use stream_context_create() to set the timeout:
The code is as follows:
$opts = array(
'http'=>array(
'method'=>"GET",
'timeout'=>60,
)
);
$context = stream_context_create($opts);
$html =file_get_contents('http://www.jb51.net', false, $context);