• Integrating Solr with PHP

    20-07-2010Author: basdenooijer

    Currently there isn’t really a standard way to integrate Solr with PHP. For various projects I have used:

    • manual HTTP requests
    • PHP Solr extension
    • solr-php-client library

    They all have their own pros and cons.

    Manual HTTP requests

    I’ve seen and used this one several times on projects that I didn’t start myself. It can be obvious with direct usage of e.g. CURL, but can also be disguised by the usage of some kind of wrapper like Zend Frameworks Zend_Http class.

    • Con: you either need to do a lot of work building a client that supports all use cases (some kind of request builder) or need to manually compose Solr requests each time. To me this seems no different to writing your own DB layer (or reinventing the wheel)
    • Con: your application is handling responses, instead of an abstracted result objects (unless you invest a lot of work in building these yourself)
    • Pro: full control over your requests (but thats only useful if you really need that, I can’t think of a case) and minimal overhead

    PHP Solr extension (http://www.php.net/manual/en/book.solr.php)

    I just installed this one on a development environment for testing, so I haven’t got much experience with it yet. But it looks very promising:

    • Pro: object oriented model of Solr communication so you don’t waste your time with low-level issues (e.g. communication with Solr)
    • Pro: PHP extension written in C, should be faster than a PHP implementation
    • Pro: documented in PHP manual
    • Con: you might not be able the use the extension, depending on your server environment
    • Con: not marked stable yet, currently still in beta

    solr-php-client library (http://code.google.com/p/solr-php-client/)

    I’ve used this library for several projects. It offers an object-oriented interface to Solr, somewhat like the PHP Solr extension. This library is however fully implemented in PHP so it can easily be used on any PHP environment.

    • Pro: object oriented model of Solr communication so you don’t waste your time with low-level issues (e.g. communication with Solr)
    • Pro: doesn’t require a special extension so should work on almost any PHP environment
    • Pro: comes with PHPdoc and has an issue tracker
    • Pro: (if you use Zend Framework) follows the Zend Framework coding style so it fits into ZF projects nicely
    • Con: the Solr model is much more basic than the PHP extension, thus requiring more own work to use features like faceting.
    • Con: might have some less performance than the other two, as the PHP extension is written in C and manual HTTP request can be fully optimized for your use case. This is an issue with almost any framework or library, but I’m talking about small differences. In reality all three solutions should perform just fine if implemented correctly.


    I would not advise to implement a Solr interface from scratch. The solr-php-client library has proven itself to me in various projects despite the somewhat limited featureset. The PHP Solr extension is making an even better impression based on my first tests, but I first want to use it for a real world project before giving a final verdict.

    All options handle the communication with Solr. They don’t offer very much beyond that point. Of course the usage of Solr can vary a lot, but there are some features that are common to many projects.
    Take for instance the usage of facets. The various libraries/extensions allow you to add a facet field or facet queries to the Solr request, but you then need to render it yourself. Later you also need to add filterqueries based on user input yourself.
    This is just one example of a basic Solr use case that currently requires a lot of coding with any of the above options. I have some ideas for a Solr component in Zend Framework style myself, but I have yet to find the time…

    Please mind that this is in no way a complete list of the options available. I’m sure more PHP implementations can be found, but these are the ones that I’m familiar with. If you know of any other (better?) alternatives, please let me know!

    I will get back on this subject after I’ve gained more experience with the PHP Solr extension. I’m also planning to do some tests to compare the options for various use cases. Some benchmarking, but also a comparison of the code needed to implement the cases. Keep posted for more.

    [UPDATE – added in march 2012]

    In the meantime I’ve created my own opensource PHP Solr client: Solarium, so there’s yet another alternative available. As I am a bit biased I encourage anyone to make their own comparison.  You can read all about it on www.solarium-project.org

    , ,
  • 6 comments on “Integrating Solr with PHP