Solr PHP client vs file_get_contents?
I am using PHP to access Solr and I wonder one thing.
开发者_StackOverflowWhy should I use Solr PHP client when I can use:
$serializedResult = file_get_contents(
'http://localhost:8983/solr/select?q=niklas&wt=phps');
to get the result in arrays and then print them out? I don't really get the difference. Are there any richer features with the PHP client?
I think because of following reasons:
- Flexibility
- Error Handling
- Security Issues
- Extendibility
All issues are the headache of these pre-made scripts and frameworks. So if there is a solution for certain thing, i would recommend you to go with that rather than doing everything from your own.
Also since these scripts and frameworks have good community support, there are lesser bugs, security issues and more.
The solr-php-client library actually has an adapter for file_get_contents (@see Apache_Solr_HttpTransport_FileGetContents), plus another for Curl. The solr-php-client library is an elaborate framework that allows you to do much more than post a URL w/ parameters so you can complete a search. SPC provides predictability, extensibility, and security. Furthermore, the solr-php-client is written to Zend Framework standards, so it compliments any ZF project.
If I have a choice between choosing a well-maintained, community-supported project, and writing my own script which will not be superior, I choose the well-maintained, community-supported project. If you have needs that are outside of that project, you can still write your own script, but again, this kind of library is build so you can extend it painlessly.
I guess that the solr client just parses the results and provides you convenient way to pass the parameters (constructing the URL for you etc.).
To use, file_get_contents()... your php must have "allow_url_open" enabled. If you host your website on certain hosts then they dont allow to use this function because of they keep this setting OFF.
In some cases, however, it might make sense to do file_get_contents(). SolrClient doesn't have anything for field collapsing (it's a patch not available in the trunk) so I have to either write my own class which mimics SolrClient and SolrQuery if I can't do it with the PECL library.
精彩评论