实际上,我想在完成后阅读搜索查询之后的内容。问题是 URL 只接受 POST
方法,它对 GET
方法没有任何 Action ...
我必须在 domdocument
或 file_get_contents()
的帮助下阅读所有内容。有什么方法可以让我用 POST
方法发送参数,然后通过 PHP
读取内容?
最佳答案
使用 PHP5 的无 CURL 方法:
$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);
有关该方法以及如何添加 header 的更多信息,请参阅 PHP 手册,例如:
- stream_context_create:http://php.net/manual/en/function.stream-context-create.php
关于php - 如何使用 PHP 发送 POST 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5647461/