1

我正在使用 php curl 库索引文件。我被困在这里的代码

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 的事件日志中没有显示任何内容。请帮我写代码

4

3 回答 3

1

$post_string您从上传的文件中读取的文件中,该文件是否以<commit />? 没有它,什么都不会提交给 Solr,因此请确保您的命令集以<commit />.

另外,我强烈不推荐这种更新文件的方式。如果有人不择手段地弄清楚您的管道以及如何运行此文件,他们很容易对您的 Solr 索引造成严重破坏。

于 2012-07-02T16:46:45.580 回答
1

这对我有用,与您的代码相同,但将 url 更改为以下

$url = "http://localhost:8983/solr/CORE_NAME/update/?commit=true";
于 2017-02-04T17:49:35.437 回答
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;
于 2015-06-05T10:33:23.050 回答