5

我正在尝试将文档添加到 ElasticSearch。我从命令行使用 curl 没有问题,但是在 PHP 中使用 curl 时遇到了问题。我正在关注文档中的这个例子: http ://www.elasticsearch.org/guide/reference/api/index_.html

以下代码给了我这个错误: {"error":"IndexMissingException[[twitter] missing]","status":404}

$search_host = '127.0.0.1';
$search_port = '9200';
$index = 'twitter';
$doc_type = 'tweet';
$doc_id = 1;

    $json_doc = array(
                "user" => "kimchy",
                "post_date" => "2012-11-15T14:12:12",
                "message" => "trying out Elastic Search"
            );
    $json_doc = json_encode($json_doc);

    $baseUri = 'http://'.$search_host.':'.$search_port.'/'.$index.'/'.$doc_type.'/'.$doc_id;

    $ci = curl_init();
    curl_setopt($ci, CURLOPT_URL, $baseUri);
    curl_setopt($ci, CURLOPT_PORT, $search_port);
    curl_setopt($ci, CURLOPT_TIMEOUT, 200);
    curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ci, CURLOPT_FORBID_REUSE, 0);
    curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'XPUT');
    curl_setopt($ci, CURLOPT_POSTFIELDS, $json_doc);
    $response = curl_exec($ci);
4

1 回答 1

9

您必须将 curl 选项设置CURLOPT_CUSTOMREQUESTPUT,而不是XPUT

curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'PUT');
于 2012-12-03T14:51:12.567 回答