In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the meaning of request context in PHP". Interested friends may wish to take a look at it. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "what is the meaning of request context in PHP"?
Knowledge of request context in PHP
First of all, let's understand what the context is. When we write articles and sentences, we will consider the logic before and after a point of view or content, and the content before and after this point of view can be regarded as its context. It contains the meaning of context in it, in fact, the context in the code world has the same meaning, and the word Context itself means environment and background.
Next, let's talk about what the request context is. For example, if we want to use PHP to request a link address, we usually use curl to make the request, but the configuration of curl is actually quite complex, so we will use the function file_get_contents () to request the link quickly in the case of simple use. However, many people may not know or use its context parameters. In fact, after using the context parameter, file_get_contents () can not only submit the POST request, but also define various request header contents. These things are the context of a request, that is, its execution environment and background.
First, we define a server that simply outputs the contents of\ $_ GET and $_ POST. At the same time, we printed $_ SERVER to see if the request header was obtained.
Print_r ($_ SERVER)
Echo 'GET INFO', PHP_EOL
Foreach ($_ GET as $k = > $v) {
Echo $k,':', $v, PHP_EOL
}
Echo PHP_EOL,PHP_EOL
Echo 'POST INFO', PHP_EOL
Foreach ($_ POST as $k = > $v) {
Echo $k,':', $v, PHP_EOL
}
Next, in our test code, we use file_get_contents () to make the POST commit.
$postdata = http_build_query (
[
'var1' = >' some content'
'var2' = >' doh'
]
);
$opts = [
'http' = > [
'method' = >' POST'
'header' = >' Content-type: application/x-www-form-urlencoded'
'content' = > $postdata
]
]
$context = stream_context_create ($opts)
$result = file_get_contents ('http://localhost:8088/?a=1', false, $context)
Print_r ($result)
Var_dump ($http_response_header)
Here, we just use stream_context_create (), and we can easily create a requested context. Stream_context_create () is a function that creates a context and receives an array of options that define the relevant options for the current request. Note that what we are actually defining here are http/https-related options, which can also define request protocol options such as ftp, socket, and so on.
After requesting the remote address using the file_get_contents () function, we can get the response header information returned by the request in the $http_response_header variable. And this variable will be defined in the current local scope, do not have to worry about global scope pollution.
The POST request can be implemented in a very simple way, and we can also use the fopen () function to achieve a similar effect, but the way to get the body and respond to the response information is different.
$url = "http://localhost:8088/?a=1";
$opts = [
'http' = > [
'method' = >' GET'
'max_redirects' = >' 0'
'ignore_errors' = >' 1'
]
]
$context = stream_context_create ($opts)
$stream = fopen ($url, 'ringing, false, $context)
/ / return the response header
Var_dump (stream_get_meta_data ($stream))
/ / return content
Var_dump (stream_get_contents ($stream))
Fclose ($stream)
In this code, we use the stream_get_meta_data () function to get the response header and stream_get_contents () to get the body of the response. In fact, this is really similar to the effect of curl, and the most important thing is that the current way of writing is more simple and convenient.
As we can see from the above code, such context-sensitive functions are Stream-type functions, that is, stream functions. They are designed to handle a variety of data, including, but not limited to, data in files, on the network, compressed files, and other operations. In the future study, we will also come into contact with other content. Today's study is actually a small part of streaming network data processing, so let's digest it first.
Test code: https://github.com/zhangyue0503/dev-blog/blob/master/php/202003/source/%E5%85%B3%E4%BA%8EPHP%E4%B8%AD%E7%9A%84%E8%AF%B7%E6%B1%82%E4%B8%8A%E4%B8%8B%E6%96%87%E7%9A%84%E7%9B%B8%E5%85%B3%E7%9F%A5%E8%AF%86.php
Reference document: https://www.php.net/manual/zh/context.php
At this point, I believe you have a better understanding of "what the request context in PHP means". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.