PHP simulates sending POST requests 4, strengthen file_get_contents() to send POST requests, filepostcontents_PHP tutorial

WBOY
Release: 2016-07-12 09:05:46
Original
929 people have browsed it

PHP simulates sending POST requests four, strengthen file_get_contents() to send POST requests, filepostcontents

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>);
Copy after login

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>
Copy after login

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>
Copy after login

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.

  • $filename: Needless to say, just fill in the URL string we want to access.
  • $use_include_path: Whether to use the path set by include_path() before the file. If used, when the file address is not found, it will automatically go to the path set by include_path() to find it. In the web address, we set it to false.
  • $context: Environmental context, resource type, set by the context returned by the function stream_context_create(), which is also the focus of our file_get_contents() function expansion. Let’s talk about it next.
  • $offset: The offset byte of the read content relative to the beginning of the file. When we read web page content, we must ensure the integrity of the HTML document, so it can be set to 0 or not set. The default is 0.
  • $maxlen: As the name suggests, it is the maximum number of bytes to read the file. We do not set the same offset and read the entire content of the web page.

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>
Copy after login

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>
Copy after login

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>); 
Copy after login

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>);
Copy after login

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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1067640.htmlTechArticlePHP simulates sending POST requests four, strengthen file_get_contents() to send POST requests, filepostcontents uses the cumbersome fsockopen() method Finally, we started looking for simpler methods in the PHP function library...
Related labels:
php
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