简体   繁体   中英

Using curl from PHP to get Elasticsearch results

I set up an Ubuntu 18.04 instance, on AWS, and installed Elasticsearch version 7 on it. I also assigned it an FQDN

I used an example, to load up the entire works of Shakespeare on it. I installed

From my Mac, using terminal, I ssh'ed into the AWS instance (using its FQDN), and from the terminal, I do the following:

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"
>                       }
>     }
> }'

I get an answer:

{
  "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:"
        }
      }
    ]
  }
}

I tried writing a PHP script, to do the same thing, as follows:

<?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);

But, I am not getting any results:

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] => 
)        

Any ideas?

this https://incarnate.github.io/curl-to-php/ or put your CURL in Postman and generate code in 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM