duankekan9269 2017-12-05 02:34
浏览 163
已采纳

将数组或对象从php发送到Elasticsearch

I want to send an array of data (if it can´t be done with arrays I wouldn´t mind converting it to object) to my Elasticsearch index. I used to send my array to a database in MySQL and then load it to Elasticsearch using logstash. I don´t need the database anymore so I was hoping to accomplish this without it, but I couldn,t find good documentation/examples for it in PHP. Any help is apreciated.

This is how I generate the array ($values[]), it has 3 parameters $exec_time (decimal), $name (text) and $date (datetime):

for($i=0; $i<50;++$i){

  $date = date('Y-m-d H:i:s');

  if($name == "local")
        $i = $i + 4;

  $ch = $_SESSION['cURL'];
  $time_pre = microtime(true);
  $data = curl_exec($ch);
  $time_pro = microtime(true);
  $exec_time = $time_pro - $time_pre;

  $values[$i] = "('$exec_time', '$name', '$date')";
}
  • 写回答

1条回答 默认 最新

  • douyousu9691 2017-12-05 05:03
    关注

    You may use elasticsearch-php, the official PHP client for Elasticsearch.
    (See the documentation).

    Here's another way to do it, using only curl:

    use Curl\Curl; // composer require php-curl-class/php-curl-class
    
    $curl = new Curl();
    $curl->setHeader('Content-Type', 'application/json');
    $response = $curl->post('http://127.0.0.1:9200/index/type', array(
      'exec_time' => ...
      'name' => ...
      'date' => ...
    ));
    $curl->close();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?