Home > php教程 > php手册 > body text

PHP模拟发送POST请求之四、加强file_get_contents()发送POST请求,filepostcontents

WBOY
Release: 2016-06-13 08:52:04
Original
976 people have browsed it

PHP模拟发送POST请求之四、加强file_get_contents()发送POST请求,filepostcontents

使用了笨重fsockopen()方法后,我们开始在PHP函数库里寻找更简单的方式来进行POST请求,这时,我们发现了PHP的文件函数也具有与远程URL交互的功能。

最简单的是fopen()和fread()函数。

<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

然后是file_get_contents()函数:

<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

但是,我们会发现,通这两种方式我们只能通过GET方式发送信息并读取网页信息,而且,这两种方式还面临着超时,无法处理头信息等问题。


不过,我们仔细查看file_get_contents()的函数原型:

<p>string file_get_contents ( string $filename [, bool $use_include_path [, resource $context [, int $offset [, int $maxlen ]]]] )</p>
Copy after login

我们发现它还有其他可选参数,我们可以通过这些参数的设置,在发送网页请求的同时,POST出我们的数据,下面来解释各个参数的意义。

  • $filename:不用多说,填写我们要访问的URL字符串就行。
  • $use_include_path:是否使用文件之前include_path()设置的路径,如果使用,在文件地址找不到时,会自动去include_path()设置的路径去寻找,网页地址中我们设置为false。
  • $context:环境上下文,resource类型,由函数 stream_context_create() 返回的 context来设置,也是我们进行file_get_contents()函数扩展的重点,接下来再说。
  • $offset:读取的内容相对文件开始内容的偏移字节,我们读取网页内容,要保证HTML文档的完整性,所以可以设置为0或者不设置,默认为0。
  • $maxlen:顾名思义,是读取文件的最大字节数,同offset我们不设置,读取网页的全部内容。

通过file_get_contents发送POST请求的重点就在$context参数上面,我们用stream_context_create()函数设置上下文。

stream_context_create()创建的上下文选项即可用于流(stream),也可用于文件系统(file system)。对于像 file_get_contents()、file_put_contents()、readfile()直接使用文件名操作而没有文件句柄的函数来说更有用。stream_context_create()增加header头只是一部份功能,还可以定义代理、超时等。

我们来看stream_context_create()函数的原型:

<p>resource stream_context_create ([ array $options [, array $params ]] )</p>
Copy after login

我们看到,通过传入设置数组用此函数来获取一个资源类型的上下文选项。

<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

设置好上下文,我们通过file_get_contents()函数进行POST数据提交。

<span>$results</span> = <span>file_get_contents</span>('http://localhost', <span>false</span>, <span>$context</span>); 
Copy after login

下面是POST请求的完整示例:

<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

如果您觉得本文对您有帮助,您可以找荐或关注我,另外,如果有什么问题,可以在下方留言讨论,谢谢。

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!