我正在尝试发出一个简单的 HTTP GET 请求来查询 Elasticsearch。 Elasticsearch 语法允许在 GET 请求中使用请求正文来添加其他查询选项。我正在使用 PHP 函数 file_get_contents() 发出 GET 请求。
问题是 file_get_contents() 在发出 GET 请求时似乎忽略了请求的正文,但在使用 POST 请求时却能正常工作。
如何让 file_get_contents() 正确处理 GET 请求的正文? 我正在使用的代码如下所示。
注意:
提前致谢!
$url = "example-elasticsearch-url-here.com/index/_search";
$body=<<<EOD
{
"_source": ["email"],
"size":10,
"query":{
"match_all" : {}
}
}
EOD;
$contextData = array (
'method' => 'GET',
'header' => "Content-Type: application/json\r\n".
'ssl'=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
'content'=> $body
);
$context = stream_context_create (array ( 'https' => $contextData ));
try {
$result = file_get_contents ($url,false,$context);
} catch (Exception $e) {
$result = false;
}
echo $result;
答案 0 :(得分:1)
如果您在 http
中使用 https
而不是 $context
作为键名,它是否有效?