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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵