This article mainly introduces the implementation of PHP using file_get_contents send HTTP request function is simple, has a certain reference value, now share to everyone, the need for friends can refer to
The example in this article tells PHP to use file_get_contents to send HTTP requests. Share to everyone for your reference, as follows:
Server-side simulation Post/get and other requests, using curl is easy to do (for example, the previous article "PHP using curl to simulate get and POST to the interface to submit and get Data"), then if you do not use the Curl Library, then what to do?
$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//timeout (in s)) ; $url = "http://www.testweb.com"; $context = Stream_ Context_create ($options); $result = File_get_contents ($url, False, $context); Echo $result;
Where the http://www.testweb.com code is:
$data = $_post;print_r ($data);
stream_context_create()
Role: Create and return a text stream and apply a variety of options that can be used fopen()
, file_get_contents()
such as the time-out setting of the process, the proxy server, the request method, and the special procedure for setting the header information.