The company I work for recently started using Solr for some of our search functionality. After downloading the files for integrating with PHP that the Wiki links to I saw that it wasn't usable for our purposes. So, as part of the project, I developed a PHP Solr Client. Coming back to the mailing list I see that there was recent talk about developing a new PHP client, but as of yet I haven't seen it posted - so I hope I'm not stepping on any toes. I received permission from my company to release the code to the community under the new BSD license. The coding style is approximately inline with the Zend Framework's standards (and somewhat PEAR's) and I feel it's well documented.

Client Requirements:
- PHP 5 >= 5.2.0 (we developed on 5.2.1) that has the json_decode function (available by default as of 5.2) and the XmlReader class (enabled by default as of 5.1) - allow_url_fopen php.ini setting must be enabled (defaults to enabled)

We'd love to have the community help us maintain this as Solr evolves. Let us know what you think. Thanks.

- Donovan Jimenez



For those interested:

The starting point is the Solr_Service class. From this class you have access to all the major functionality of the Solr HTTP service (add, delete by id, delete by query, commit, optimize and search). Below I've include a sample of the client's API for searching:


<?php
require_once('Solr/Service.php');

$start = microtime(true);

$solr = new Solr_Service(); //Or explicitly new Solr_Service ('localhost', 8180, '/solr');

try
{
        $response = $solr->search('solr', 0, 10,
                array(/* you can include other parameters here */));

echo 'search returned with status = ', $response- >responseHeader->status, ' and took ', microtime(true) - $start, ' seconds', "\n";

        //here's how you would access results
//Notice that I've mapped the values by name into a tree of stdClass objects //and arrays (actually most of this is done by json_decode if the response is
        //in json format - done by an XmlReader loop if its XML)
        if ($response->response->numFound > 0)
        {
                $doc_number = $response->response->start;

                foreach ($response->response->docs as $doc)
                {
                        $doc_number++;

                        echo $doc_number, ': ', $doc->text, "\n";
                }
        }

//for the purposes of seeing the available structure of the response //NOTE: Solr_Response::_parsedData is lazy loaded, so a print_r on the response before //any values are accessed may result in different behavior (in case
        //anyone has some troubles debugging)
        //print_r($response);
}
catch (Exception $e)
{
        echo $e->getMessage(), "\n";
}

?>

Reply via email to