如何使用 PHP 发送 POST 请求? [英] How do I send a POST request with PHP?
问题描述
实际上,我想在搜索查询完成后阅读内容.问题是 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()
的帮助下阅读所有内容.有没有什么方法可以让我用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
",
'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屋!