elasticsearch用php和curl更新文档

时间:2014-06-16 11:11:01

标签: php curl elasticsearch

我尝试用PHP和curl更新我的一个文档。 我有这个源代码:

     protected function call($url, $method="GET", $payload=null) {
     $conn = $this->ch;
     $protocol = "http";
     $requestURL = $protocol . "://" . $this->host .":9200". $url;
     curl_setopt($conn, CURLOPT_URL, $requestURL);
     curl_setopt($conn, CURLOPT_TIMEOUT, self::TIMEOUT);
     curl_setopt($conn, CURLOPT_PORT, $this->port);
     curl_setopt($conn, CURLOPT_RETURNTRANSFER, 1) ;
     curl_setopt($conn, CURLOPT_CUSTOMREQUEST, strtoupper($method));
     curl_setopt($conn, CURLOPT_FORBID_REUSE , 0) ;

     if (is_array($payload) && count($payload) > 0)
    {
        echo "<br/>[-] json_encode";
        curl_setopt($conn, CURLOPT_POSTFIELDS, json_encode($payload));
    }
     else
     {
        curl_setopt($conn, CURLOPT_POSTFIELDS, array($payload));
     }

     $response = curl_exec($conn);
     if ($response !== false) {
         $data = json_decode($response, true);
         if (!$data) {
             $data = array('error' => $response, "code" => curl_getinfo($conn, CURLINFO_HTTP_CODE));
         }
     }
     else { //... error } }

等...}

&#34; $ payload&#34;,对应于&#34; $ value [&#34; title&#34;] =&#39; Shining&#39;;&#34;我看到了json_encode ......但是我发现了这个错误:

  

[ - ] json_encode {&#34;错误&#34;:&#34; ActionRequestValidationException [验证   失败:1:缺少脚本或文档;]&#34;,&#34; status&#34;:500}

如何解决此错误。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

你可以使用elasticsearch库......它真的很棒。

https://github.com/elasticsearch/elasticsearch