Asif S. Abid
Asif S. Abid

Reputation: 57

indexing documents in Apache Solr using php-curl library

I am indexing the file using php curl library. I am stuck here with the code

echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
     $result=move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]);
     if ($result == 1) echo "<p>Upload done .</p>";
        $options = getopt("f:");
        $infile = $options['f'];

        $url = "http://localhost:8983/solr/update/";
        $filename = "upload/" . $_FILES["file"]["name"];
        $handle = fopen($filename, "rb");
        $contents = fread($handle, filesize($filename));
        fclose($handle);
        echo $url;
        $post_string = file_get_contents("upload/" . $_FILES["file"]["name"]);
        echo $contents;
        $header = array("Content-type:text/xml; charset=utf-8");

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLINFO_HEADER_OUT, 1);

        $data = curl_exec($ch);

        if (curl_errno($ch)) {
           print "curl_error:" . curl_error($ch);
        } else {
           curl_close($ch);
           print "curl exited okay\n";
           echo "Data returned...\n";
           echo "------------------------------------\n";
           echo $data;
           echo "------------------------------------\n";
        }

Nothing is showing as a result. Moreover there is nothing shown in the event log of Apache Solr. please help me with the code

Upvotes: 1

Views: 2094

Answers (3)

Matrix
Matrix

Reputation: 1840

This worked for me, same as your code, but change the url to the following

$url = "http://localhost:8983/solr/CORE_NAME/update/?commit=true";

Upvotes: 1

Aviator
Aviator

Reputation: 522

Here is how it works for me:

$target_url = $config['core']['solr_host'] . "/solr/update/extract?commit=true&literal.id=" . urlencode ( $page ['id'] ) . "&literal.url=" . urlencode ( rtrim(BASE_URL . $page ['path'],"/") );
$file_name_with_full_path = WEBSITE_PATH . $page ['path'];

$post = array (
   'myFile' => '@' . $file_name_with_full_path 
);

$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $target_url );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $post );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_USERPWD, "user:pass" ); // for Apache Basic Auth


$result = curl_exec ( $ch );
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close ( $ch );

echo $result;

Upvotes: 0

Ansari
Ansari

Reputation: 8218

In the $post_string that you read from your uploaded file, does that file end with a <commit />? Without that nothing will get committed to Solr so make sure your set of commands ends with <commit />.

Also, I would highly not recommend this way of updating files. If someone unscrupulous figures out your pipeline and how to run this file, they can easily wreak havoc with your Solr index.

Upvotes: 1

Related Questions