After using the cumbersome fsockopen() method, we started looking for more information in the PHP function library A simple way to make a POST request. At this time, we discovered that PHP's file function also has the function of interacting with remote URLs.
The simplest ones are the fopen() and fread() functions.
<span>$fp</span>=<span>fopen</span>('http://localhost?query=query','r'<span>); </span><span>$content</span>=<span>fread</span>(<span>$fp</span>,1024<span>); </span><span>echo</span> <span>$content</span>;<span>//</span><span>输出HTML文档信息</span> <span>fclose</span>(<span>$fp</span>);
Then the file_get_contents() function:
<span>$content</span>=<span>file_get_contents</span>('http://localhost?query=query'<span>); </span><span>echo</span> <span>$content</span>;<span>//</span><span>输出HTML文档信息</span>
However, we will find that through these two methods, we can only send information and read web page information through GET. Moreover, these two methods also face problems such as timeout and inability to process header information.
However, let’s take a closer look at the function prototype of file_get_contents():
<p>string file_get_contents ( string $filename [, bool $use_include_path [, resource $context [, int $offset [, int $maxlen ]]]] )</p>
We found that it has other optional parameters. By setting these parameters, we can POST out our data while sending the web page request. The meaning of each parameter is explained below.
The focus of sending a POST request through file_get_contents is the $context parameter. We use the stream_context_create() function to set the context.
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.
Let’s look at the prototype of the stream_context_create() function:
<p>resource stream_context_create ([ array $options [, array $params ]] )</p>
We see that this function is used to obtain the context options of a resource type by passing in the settings array.
<span>$context</span> = <span>stream_context_create</span>(<span>array</span>( <span>//</span><span>传入数组类型的$option参数</span> 'http' => <span>array</span>( <span>//</span><span>以HTTP请求为键的设置数组</span> 'method' => 'POST', <span>//</span><span>设置请求方法为POST</span> 'header' => "Content-type: application/x-www-form-urlencoded",<span>//</span><span>通过设置头文件来设置POST数据格式</span> 'content' => <span>http_build_query</span>(<span>$query_info</span>), <span>//</span><span>用http_build_query()方法将数组拼合成数据字符串</span> 'timeout' => 20 <span>//</span><span>设置请求的超时时间。</span> <span> ) )); </span>
After setting the context, we submit POST data through the file_get_contents() function.
<span>$results</span> = <span>file_get_contents</span>('http://localhost', <span>false</span>, <span>$context</span>);
Here is a complete example of a POST request:
<span>$info</span>=['eat'=>'2kg','run'=>'10km'<span>] ; </span><span>$url</span>='http://localhost'<span>; </span><span>$context</span> = <span>stream_context_create</span>(<span>array</span><span>( </span>'http' => <span>array</span><span>( </span>'method' => 'POST', 'header' => 'Content-type:application/x-www-form-urlencoded', 'content' => html_build_query(<span>$info</span>), 'timeout' => 20<span> ) )); </span><span>$result</span> = <span>file_get_contents</span>(<span>$url</span>, <span>false</span>, <span>$context</span>);
If you think this article is helpful to you, you can recommend it or follow me. In addition, if you have any questions, you can leave a message below for discussion. Thank you.