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

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

问题描述:

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

$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:
<pre>", htmlspecialchars($verboseLog), "</pre>
";
//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.

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.

Thanks in advance for your help !!!

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

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