如何使用PHP发送POST请求? [英] How do I send a POST request with PHP?

查看:134
本文介绍了如何使用PHP发送POST请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上我想阅读搜索查询后的内容,完成后。问题是URL只接受 POST 方法,并且它不会对 GET 方法采取任何操作...

Actually I want to read the contents that come after the search query, when it is done. The problem is that the URL only accepts POST methods, and it does not take any action with GET method...

我必须在 domdocument file_get_contents()<的帮助下阅读所有内容/ code>。是否有任何方法可以让我使用 POST 方法发送参数,然后通过 PHP 读取内容?

I have to read all contents with the help of domdocument or file_get_contents(). Is there any method that will let me send parameters with POST method and then read the contents via PHP?

推荐答案

使用PHP5的无CURL方法:

CURL-less method with PHP5:

$url = 'http://server.com/path';
$data = array('key1' => 'value1', 'key2' => 'value2');

// use key 'http' even if you send the request to https://...
$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data)
    )
);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */ }

var_dump($result);

有关方法以及如何添加标题的详细信息,请参阅PHP手册,例如:

See the PHP manual for more information on the method and how to add headers, for example:

  • stream_context_create: http://php.net/manual/en/function.stream-context-create.php

这篇关于如何使用PHP发送POST请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆