Home  >  Article  >  Backend Development  >  PHP uses stream_context_create() to simulate POST/GET request method

PHP uses stream_context_create() to simulate POST/GET request method

高洛峰
高洛峰Original
2017-01-06 15:10:221501browse

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;

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

$data = $_POST;
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r( $data );
echo &#39;
';

The running result is:

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

Some key points to explain:

1. The above program uses http_build_query() Function, if you need to know more about it, 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(
    &#39;http&#39; => array(
        &#39;method&#39; => &#39;POST&#39;,
        &#39;header&#39; => sprintf("Authorization: Basic %s\r\n", base64_encode($username.&#39;:&#39;.$password)).
        "Content-type: application/x-www-form-urlencoded\r\n",
        &#39;content&#39; => http_build_query(array(&#39;status&#39; => $message)),
        &#39;timeout&#39; => 5,
    ),
));
$ret = file_get_contents(&#39;http://twitter.com/statuses/update.xml&#39;, false, $context);

3. The context options created by stream_context_create can be used for both streams and file systems. (file system). 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. special process.

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

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

More PHP uses stream_context_create() to simulate POST/GET For articles related to the request method, please pay attention to the PHP Chinese website!

Statement:
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