Home >Backend Development >PHP Tutorial >PHP uses stream_context_create() to simulate POST/GET request method and example analysis

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

墨辰丷
墨辰丷Original
2018-06-04 10:35:321913browse

This article mainly introduces the method of PHP using stream_context_create() to simulate POST/GET requests. It analyzes the principle of stream_context_create to simulate POST/GET requests in detail in the form of examples, the usage method and related precautions. Friends in need can Refer to the next

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;


http:/ The code of /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 the http_build_query() function. If you need to know more, you can refer to the previous article "PHP uses http_build_query()" Method to construct 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 streams. , can also be used in 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, which 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()前使用

Summary: The above is the entire content of this article, I hope it can help Everyone’s learning helps.

Related recommendations:

phpQuick sorting principle, implementation method and example analysis

PHPExcel Methods and simple examples to implement reading excel files


##PHP MVC framework skymvc supports multiple file upload implementation methods


The above is the detailed content of PHP uses stream_context_create() to simulate POST/GET request method and example analysis. For more information, please follow other related articles on 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