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 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签)
  • ¥50 sft下载大文阻塞卡死
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失
  • ¥15 springboot+vue 集成keycloak sso到阿里云
  • ¥15 win7系统进入桌面过一秒后突然黑屏
  • ¥30 backtrader对于期货交易的现金和资产计算的问题