duankekan9269 2017-12-05 10: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 13: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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 clousx6整点报时指令怎么写
  • ¥30 远程帮我安装软件及库文件
  • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
  • ¥20 深信服vpn-2050这台设备如何配置才能成功联网?
  • ¥15 Arduino的wifi连接,如何关闭低功耗模式?
  • ¥15 Android studio 无法定位adb是什么问题?
  • ¥15 C#连接不上服务器,
  • ¥15 angular项目错误
  • ¥20 需要帮我远程操控一下,运行一下我的那个代码,我觉得我无能为力了
  • ¥20 有偿:在ubuntu上安装arduino以及其常用库文件。