PHP uses file_get_contents to send HTTP requests with simple functionality

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.