How PHP uses Stream_context_create () to simulate Post/get requests _php tips

Source: Internet
Author: User
Tags curl

The example in this article describes how PHP uses Stream_context_create () to simulate post/get requests. Share to everyone for your reference, specific as follows:

Sometimes, we need to simulate post/get in the server-side request, that is, in the PHP program to implement the simulation, how to do it? Or, in a PHP program, give you an array, how do you post/get this array to another address? Of course, it's easy to use CURL, so what if you don't use the CURL library? In fact, in PHP already has the related function realization, this function is next to speak of Stream_context_create ().

Direct Show your code, this is the best way:

$data = Array (
    ' foo ' => ' Bar '), 
    ' baz ' => ' boom ', 
    ' site ' => ' localhost ', 
    ' name ' => ' Nowa Magic ' ); 
$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 * 60//Timeout (unit: s))
);
$url = "http://localhost/test2.php";
$context = Stream_context_create ($options);
$result = File_get_contents ($url, False, $context);
echo $result;

The http://localhost/test2.php code is:

$data = $_post;
Echo ' <pre> ';
Print_r ($data);
Echo ' </pre> ';

The results of the operation are:

Array
(
  [foo] => bar
  [Baz] => boom
  [site] => localhost
  [name] => Nowa (Magic
)

Some points to explain:

1. The above procedure uses the Http_build_query () function, if needs to understand, may refer to the previous article "PHP uses Http_build_query () constructs the URL string method".

2. Stream_context_create () is used to create the upper and lower file options for open files, such as post access, using proxies, sending headers, and so on. is to create a stream and give an example:

$context = stream_context_create (Array (' 
    http ' => Array ( 
        ' method ' => ' POST ', 
        ' header ' => sprintf ( "Authorization:basic%s\r\n", Base64_encode ($username. ': '. $password)). 
        " content-type:application/x-www-form-urlencoded\r\n ", 
        ' content ' => http_build_query (Array (' status ' => $ Message)), 
        ' timeout ' => 5,) 
; 
$ret = file_get_contents (' Http://twitter.com/statuses/update.xml ', false, $context); 

3. The contextual items created by Stream_context_create can be used for streaming (stream) or for the file system. is more useful for functions like file_get_contents, file_put_contents, readfile that use file names directly without file handles. Stream_context_create Increase header header is only a part of the function, you can also define agents, timeouts, and so on. This makes access to the Web less powerful than curl.

4. Stream_context_create () function: Create and return a text data stream and apply various options, which can be used for fopen (), file_get_contents (), and other procedures such as timeout settings, proxy server, request mode, header information set special procedures.

5. Stream_context_create can also resolve the file_get_contents timeout process by adding the timeout option:

$opts = Array ('
  http ' =>array (
  ' method ' => ' get ',
  ' timeout ' =>60,
 )
);
Create a data flow context
$context = Stream_context_create ($opts);
$html =file_get_contents (' http://localhost ', false, $context);
fopen all remaining data at the output file pointer:
//fpassthru ($FP),//fclose () before using

More interested in PHP related content readers can view the site topics: "PHP Operations and Operator Usage Summary", "PHP Network Programming Skills Summary", "PHP Basic Grammar Introductory Tutorial", "PHP operation Office Document Skills Summary (including word,excel,access, PPT), "The PHP date and time usage summary", "PHP object-oriented Programming Introduction Course", "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

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.