Home >Backend Development >PHP Tutorial >PHP uses file_get_contents to send http requests
The main content of this article is about PHP using file_get_contents to send http requests. It has a certain reference value. Now I share it with everyone. Friends in need can refer to it
Server-side simulation POST /GET and other requests, are easy to do using CURL, so what should we do if we don’t use the CURL library?
$data = array( 'test'=>'bar', 'baz'=>'boom', 'site'=>'www.nimip.com', 'name'=>'nimip.com'); $data = http_build_query($data); //$postdata = http_build_query($data); $options = array( 'http' => array( 'method' => 'POST', 'header' => 'Content-type:application/x-www-form-urlencoded', 'content' => $data 'timeout' => 60 // 超时时间(单位:s) ) ); $url = "http://www.nimip.com"; $context = stream_context_create($options); $result = file_get_contents($url, false, $context); echo $result;
The code for http://www.nimip.com is:
$data = $_POST; print_r( $data );
stream_context_create() Function: Create and return A text data stream and applying various options, which can be used for special processes such as timeout settings, proxy servers, request methods, and header information settings for processes such as fopen() and file_get_contents().
Related recommendations:
PHP uses qqwry.bat to obtain the actual address
The above is the detailed content of PHP uses file_get_contents to send http requests. For more information, please follow other related articles on the PHP Chinese website!