pi-2r
pi-2r

Reputation: 1279

elasticsearch update document with php and curl

I try to update one of my document with PHP and curl. I've got this source code:

     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 } }

etc ...}

"$payload", corresponding at "$value["title"] = 'Shining';" and i see json_encode... but I've got this error:

[-] json_encode{"error":"ActionRequestValidationException[Validation Failed: 1: script or doc is missing;]","status":500}

How to resolve this error. thanks in advance.

Upvotes: 1

Views: 1127

Answers (1)

Lachezar Todorov
Lachezar Todorov

Reputation: 923

You can use the elasticsearch library ... it's really good.

https://github.com/elasticsearch/elasticsearch

Upvotes: 1

Related Questions