2012-07-02 45 views
1

我使用php curl庫爲文件建立索引。我被卡在這裏的代碼使用php-curl庫在Apache Solr中建立索引文件

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

沒有顯示結果。此外,Apache Solr的事件日誌中沒有任何顯示。 請幫助我的代碼

回答

1

在您從上傳的文件中讀取$post_string,確實該文件與<commit />結束?沒有這些,Solr就沒有任何東西可用,所以請確保你的命令集以<commit />結尾。

此外,我極力不推薦這種更新文件的方式。如果有人肆無忌憚地指出你的管道和如何運行這個文件,他們可能會輕易地破壞你的Solr索引。

+0

@AsifSAbid這樣做對你的工作? – Ansari

0

這裏是如何爲我的作品:

$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; 
1

這個工作對我來說,與您的代碼,但是更改URL以下

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