PHP utility Function Manual: Stream_context_create () Analog post/get

Source: Internet
Author: User

PHP utility Function Manual: Stream_context_create () Analog post/get

Sometimes, we need to simulate post/get on the server side, 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, there are already related functions implemented in PHP, this function is the next stream_context_create ().

Direct show you the code, this is the best way:

$data = Array (' foo ' = ' bar ', ' baz ' = ' boom ', ' site ' = ' www.lai18.com ', ' name ' = ' lai18 '); $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 (in s)); $url = " http://www.lai18.net/"; $context = Stream_context_create ($options); $result = File_get_contents ($url, False, $context) ; Echo $result;

The code for http://www.lai18.com is:

$data = $_post;echo ' <pre> ';p rint_r ($data); Echo ' </pre> ';


The result of the operation is:

Array (    [foo] = bar    [Baz] = Boom    [site] = www.lai18.com    [name] = lai18)


Some key explanations (ref.: http://www.lai18.com/content/425476.html):

1. The above procedure uses the Http_build_query () function, if you need to know, you can see the stream), can also be used in the file system. It is more useful for functions such as file_get_contents, File_put_contents, ReadFile to use file name operations directly without the handle of the files. Stream_context_create added header header is only a part of the function, you can also define agents, timeouts and so on. This makes the ability to access the Web to be not weaker than curl.

4. Stream_context_create () Role: Create and return a text stream and apply various options that can be used for fopen (), file_get_contents () and other procedures such as the time-out settings, Proxy server, request method, header information set up a special process.

5. Stream_context_create can also resolve file_get_contents timeout processing 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://www.lai18.com ', false, $context);// fopen all remaining data at the output file pointer://fpassthru ($FP); Fclose () before use
Read the following list of topics in this article:

1PHP function Completion: GET_MAGIC_QUOTES_GPC ()

2PHP function Completion: error_reporting ()

3PHP function Completion: Array_splice ()

4PHP function Completion: Isset ()

5PHP function Completion: Array_multisort ()

6PHP function Completion: UrlEncode ()

7PHP function Completion: Preg_match ()

8PHP function Completion: header ()

9PHP function Completion: getenv ()

10PHP function Completion: List ()

11PHP function Completion: mysql_num_rows ()

12PHP function Completion: Explode ()

13PHP function Completion: mysql_fetch_array ()

14PHP function Completion: Number_format ()

15PHP function Completion: mysql_query ()

16PHP function Completion: Empty ()

17PHP function Completion: Call_user_func ()

18PHP function Completion: Parse_url ()

19PHP function Completion: Import_request_variables ()

20PHP function Completion: Imagecopyresamples ()

21PHP function Completion: Remove HTML tag strip_tags ()

22PHP function Completion: output array structure and content var_dump ()

23PHP function Completion: Var_export ()

24PHP function Completion: Determine whether the variable is a digital is_numeric ()

25PHP function Completion: http_build_query () constructs URL string

26PHP utility Function Manual: Stream_context_create () Analog post/get

27PHP function Completion: Shuffle () take an array of several random elements

28PHP function Completion: session_id ()

29PHP function Completion: NL2BR () and nl2p () function

30PHP function Completion: Session_name ()

Ignore_user_abort function Realization method of 31php planning task

How to use the Error_log () function in 32PHP

33PHP utility Function-array_change_key_case () converts the key of an array to uppercase and lowercase

34PHP utility Function-function_exists () to detect if a function is defined

35PHP utility Function-array_intersect () computes the intersection of two or more arrays

36PHP utility Function-array_diff () computes the difference between arrays

37PHP utility Function-compact () converts a known variable to an array

38PHP utility Function-extract () converts an array into a corresponding key-value pair variable

39PHP utility function-array_count_values () counts the number of occurrences of an element in an array

40PHP utility Function-getopt () Get options from the command line argument list

41PHP Utility function String function classification

42 using PHP's own filter function for data validation

PHP utility Function Manual: Stream_context_create () Analog post/get

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.