[英]Using curl from PHP to get Elasticsearch results
我在AWS上设置了Ubuntu 18.04实例,并在其上安装了Elasticsearch版本7。 我还给它分配了一个FQDN
我举了一个例子,将莎士比亚的全部作品载入其中。 我安装了
在Mac上,使用终端,将其SSH到AWS实例(使用其FQDN),然后在终端中,执行以下操作:
curl -H "Content-Type: application/json" -XGET '127.0.0.1:9200/shakespeare/_search?pretty' -d'
> {
> "query":
> {
> "match_phrase": {
> "text_entry":"to be or not to be"
> }
> }
> }'
我得到一个答案:
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 13.889601,
"hits" : [
{
"_index" : "shakespeare",
"_type" : "_doc",
"_id" : "34229",
"_score" : 13.889601,
"_source" : {
"type" : "line",
"line_id" : 34230,
"play_name" : "Hamlet",
"speech_number" : 19,
"line_number" : "3.1.64",
"speaker" : "HAMLET",
"text_entry" : "To be, or not to be: that is the question:"
}
}
]
}
}
我尝试编写PHP脚本,以执行相同的操作,如下所示:
<?php
$query = '{
"query":
{"match_phrase":{
"text_entry":"to be or not to be"
}
}
}';
$url = 'http://myserver.com:9200?shakespeare/_search?pretty';
$method = "GET";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method));
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
$result = array('info' => curl_getinfo($ch), 'op_result' => json_decode(curl_exec($ch), TRUE));
print_r($result);
但是,我没有得到任何结果:
Array
(
[info] => Array
(
[url] => http://myserver.com:9200?shakespeare/_search?pretty
[content_type] =>
[http_code] => 0
[header_size] => 0
[request_size] => 0
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0
[namelookup_time] => 0
[connect_time] => 0
[pretransfer_time] => 0
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => -1
[upload_content_length] => -1
[starttransfer_time] => 0
[redirect_time] => 0
[redirect_url] =>
[primary_ip] =>
[certinfo] => Array
(
)
[primary_port] => 0
[local_ip] =>
[local_port] => 0
)
[op_result] =>
)
有任何想法吗?
https://incarnate.github.io/curl-to-php/或将CURL放在Postman中并在PHP中生成代码
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '127.0.0.1:9200/shakespeare/_search?pretty');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "\n{\n\"query\":\n {\n \"match_phrase\": {\n \"text_entry\":\"to be or not to be\"\n }\n }\n}");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.