Home >Backend Development >PHP Tutorial >Usage example of PHP stream_context_create() function, createfile function_PHP tutorial
stream_context_create() function is used to create context file options for opening files, used for fopen(), file_get_contents() Special processes such as timeout settings, proxy servers, request methods, and header information settings.
For example, in the last PHP tutorial, the gd library implements downloading of all pictures on the web page, line 10:
Using stream_context_create() to set the proxy server:
Copy code 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:
Copy code The code is as follows:
$opts = array(
'http'=>array(
'method'=>"GET",
'timeout'=>60,
)
);
$context = stream_context_create($opts);
$html =file_get_contents('http://www.bkjia.com', false, $context);