Author: y0umer this function is known to anyone familiar with PHP. It can obtain local content or support remote content capturing through HTTP or FTP. However, file_get_contents is discarded when an HTTP header or COOKIE is sent. After in-depth research by the husband, we found that the file_get_contents function can also send HTTP headers. Function prototype: String file_get_contents (string $ filename [, bool $ use_include_path [, resource $ context [, int $ offset [, int $ maxlen]) the first parameter is the file name, and the second parameter is whether it is in php. if the init_path function is configured in ini, the third parameter can send an HTTP header. For details, see the code: <? Php $ options = array ('http' => array ('cookie '=> 'phpsessionid = 1231467477', 'method' => 'get ')); $ context = stream_context_create ($ options); $ code = file_get_contents (' http://www.baidu.com/index.php ', False, $ context);?> Code explanation: function prototype: Resource stream_context_create ([array $ options [, array $ params]) stream_context_create creates and returns a text data stream and applies various options for fopen (), file_get_contents. In this way, our file_get_contents function has the ability to send HTTP header, of course, can also be used for fopen and other functions. We will not talk much about it. The following code adds a timeout value to the file_get_contents function: <? Php $ options = array ('http' => array ('timeout' => 10 // 10 ms); $ context = stream_context_create ($ options ); $ code = file_get_contents (' http://www.baidu.com/index.php ', False, $ context);?> Combined with the above instance, a safe dog exp php version: <? Php $ url = empty ($ _ GET ['url'])? $ _ GET ['url']: ''; $ options = array ('http' => array ('user-agent' => 'baidu' // forge a browser into a hundred-degree spider .)); $ context = stream_context_create ($ options); $ code = file_get_contents ($ url, false, $ context); Echo $ code;?> In fact, fsockopen and socket functions are similar. But file_get_contents is recommended ..References:Http://www.bkjia.com/kf/201305/209937.html
Http://www.bkjia.com/net/201305/209939.html