php - 如何使用 PHP 发送 POST 请求?

实际上,我想在完成后阅读搜索查询之后的内容。问题是 URL 只接受 POST 方法,它对 GET 方法没有任何 Action ...

我必须在 domdocumentfile_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

https://stackoverflow.com/questions/5647461/

相关文章:

php - 绕过 mysql_real_escape_string() 的 SQL 注入(injec

php - 如何在 PHP 中将字符串转换为数字?

php - 查找两个日期之间的天数

php - 如何在 PHP 中获得有用的错误消息?

php - PHP中@符号的用途是什么?

php - fatal error : Allowed Memory Size of 1342177

php - 如果文件夹不存在,则创建一个文件夹

php - 如何将 var_dump 的结果捕获到字符串?

php - 在 PHP 中使用前导零格式化数字

php - 如何在 PHP 中去除字符串中的所有空格?