通过cURL发送JSON后,Solr PHP错误请求

时间:2014-02-02 21:20:48

标签: php json curl solr

我正在尝试通过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"}}]'

响应:

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

安静得很好。 之后我尝试了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<pre>", htmlspecialchars($verboseLog), "</pre>\n";
//get server response

die(pr($result));

服务器响应:

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

为什么Solr说错误的请求 - &gt;未知命令:url? 我的文件是否需要转义字符串?我已经验证了json格式。

首先,我尝试使用更复杂的json文件。我还尝试设置标题信息的内容长度。但这是由cURL自动设置的。

先谢谢你的帮助!!!

1 个答案:

答案 0 :(得分:0)

您需要添加要在solr上执行的命令。 E.g。

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