[英]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.