php curl solr,通過cURL發送JSON后,Solr PHP Bad Request

璩慎之
2023-12-01

I am trying to update fields in my solr documents via cURL, because a partial update method is not available in Apache's Solr PHP Client. Before i tried the PHP cURL API, I tried it manually via terminal:

我試圖通過cURL更新我的solr文檔中的字段,因為Apache的Solr PHP客戶端中沒有部分更新方法。在我嘗試PHP cURL API之前,我通過終端手動嘗試了它:

curl http://[server-ip]:8080/solr/collection1/update -H 'Content-type:application/json'd-d '[{"url":"https:\/\/hrz1.rz.htw-berlin.de\/oneNet\/NetStorage\/Common%20HRZ\/Dozent\/FB4\/Classen\/DAWeb\/crawling12.pdf","titel":"Crawling - Ingo Cla\u00dfen HTW Berlin-8-8-1","keywords":{"add":["cla\u00dfen","peter pan","htw-berlin"]},"bewertung":{"set":"5"},"schwierigkeit":{"set":"5"}}]'

Response:

{"responseHeader":{"status":0,"QTime":9}}

It works quiet well. After that i tried the PHP API.

它很安靜。之后我嘗試了PHP API。

$exampledata = array("url" => 'google.de', "titel" => "peter");

//make a json file

$update = json_encode($exampledata);

pr($update);

//send json file to solr-server

$curlSolrUpdate = curl_init();

curl_setopt($curlSolrUpdate, CURLOPT_URL, "http://[server-ip]:8080/solr/collection1/update");

//curl_setopt($curlSolrUpdate, CURLOPT_POST, 1);

curl_setopt($curlSolrUpdate,CURLOPT_POST,true);

curl_setopt($curlSolrUpdate, CURLOPT_POSTFIELDS, $update);

//curl_setopt($curlSolrUpdate, CURLINFO_HEADER_OUT, true);

curl_setopt($curlSolrUpdate, CURLOPT_RETURNTRANSFER, true);

curl_setopt($curlSolrUpdate, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));

curl_setopt($curlSolrUpdate, CURLOPT_VERBOSE, true);

$verbose = fopen('php://temp', 'rw+');

curl_setopt($curlSolrUpdate, CURLOPT_STDERR, $verbose);

$result = curl_exec($curlSolrUpdate);

rewind($verbose);

$verboseLog = stream_get_contents($verbose);

echo "Verbose information:\n

", htmlspecialchars($verboseLog), "
\n";

//get server response

die(pr($result));

Server-Response:

Verbose information:

* About to connect() to [server-ip] port 8080 (#0)

* Trying [server-ip]...

* connected

* Connected to 1[server-ip] ([server-ip]) port 8080 (#0)

> POST /solr/collection1/update HTTP/1.1

Host: [server-ip]:8080

Accept: */*

Content-Type:application/json

Content-Length: 35

* upload completely sent off: 35 out of 35 bytes

* HTTP 1.1 or later with persistent connection, pipelining supported

< HTTP/1.1 400 Bad Request

< Server: Apache-Coyote/1.1

< Content-Type: text/plain;charset=UTF-8

< Transfer-Encoding: chunked

< Date: Sun, 02 Feb 2014 20:51:43 GMT

< Connection: close

<

* Closing connection #0

{"responseHeader":{"status":400,"QTime":2},"error":{"msg":"Unknown command: url [6]","code":400}}

So why does Solr says bad request -> unknown command: url ? Does my file need to get escaped strings? I already validated the json format.

那么Solr為什么說錯誤請求 - >未知命令:url?我的文件是否需要轉義字符串?我已經驗證了json格式。

First i tried with a much more complex json file. I also tried to set the length of the content for header information. But this is being set automatically by cURL.

首先,我嘗試使用更復雜的json文件。我還嘗試設置標題信息的內容長度。但這是由cURL自動設置的。

Thanks in advance for your help !!!

在此先感謝您的幫助 !!!

1 个解决方案

#1

0

You need to add the command to be executed on solr. E.g.

您需要添加要在solr上執行的命令。例如。

$exampledata = array('add' => array('doc' => array("url" => 'google.de', "titel" => "peter")));

 类似资料: