Home > Backend Development > PHP Tutorial > PHP uses stream_context_create() to simulate POST/GET requests_php tips

PHP uses stream_context_create() to simulate POST/GET requests_php tips

WBOY
Release: 2016-05-16 19:54:57
Original
1483 people have browsed it

The example in this article describes how PHP uses stream_context_create() to simulate POST/GET requests. Share it with everyone for your reference, the details are as follows:

Sometimes, we need to simulate POST/GET and other requests on the server side, that is, to implement the simulation in the PHP program. How to do it? In other words, in a PHP program, if you are given an array, how do you POST/GET this array to another address? Of course, it's easy to do it using CURL, but what if you don't use the CURL library? In fact, there is already a related function implemented in PHP, and this function is stream_context_create() that I will talk about next.

Show you the code directly, 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 // 超时时间(单位:s)
    )
);
$url = "http://localhost/test2.php";
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
echo $result;

Copy after login

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

$data = $_POST;
echo '<pre class="brush:php;toolbar:false">';
print_r( $data );
echo '
';
Copy after login

The running result is:

Array
(
  [foo] => bar
  [baz] => boom
  [site] => localhost
  [name] => nowa magic
)

Copy after login

Some key points:

1. The above program uses the http_build_query() function. If you need to know more, you can refer to the previous article "How PHP uses http_build_query() to construct a URL string".

2. stream_context_create() is used to create context options for opening files, such as accessing with POST, using a proxy, sending headers, etc. Just create a stream. Let’s give another 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); 

Copy after login

3. The context options created by stream_context_create can be used for both streams and file systems. It is more useful for functions like file_get_contents, file_put_contents, and readfile that operate directly on file names without file handles. Adding headers to stream_context_create is only part of the function. You can also define proxies, timeouts, etc. This makes the function of accessing the web not weaker than curl.

4. The function of stream_context_create(): Create and return a text data stream and apply various options. It can be used for timeout settings, proxy servers, request methods, and header information settings of fopen(), file_get_contents() and other processes. .

5. stream_context_create can also solve file_get_contents timeout processing by adding the timeout option:

$opts = array(
  'http'=>array(
  'method'=>"GET",
  'timeout'=>60,
 )
);
//创建数据流上下文
$context = stream_context_create($opts);
$html =file_get_contents('http://localhost', false, $context);
//fopen输出文件指针处的所有剩余数据:
//fpassthru($fp); //fclose()前使用

Copy after login

Readers who are interested in more PHP-related content can check out the special topics on this site: "Summary of PHP operations and operator usage", "Summary of PHP network programming skills", " Introductory tutorial on PHP basic syntax", "Summary of PHP office document operation skills (including word, excel, access, ppt)", "Summary of PHP date and time usage》, "php object-oriented programming introductory tutorial", "php string (string) usage summary", "php mysql database operation introductory tutorial" And "Summary of common database operation skills in PHP"

I hope this article will be helpful to everyone in PHP programming.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template