Using PHP cURL to Send Queries to Solr

    Aparna Gawade
Listen to this article

Even though the Solr search platform offers advanced search capabilities, many of us are apprehensive towards working with it. Mainly because it is a bit complicated. A Solr Client makes the job a bit easier, but at times, you may encounter some problems.

PHP cURL Query

For faster search, Solr uses indexing. Indexing is basically classifying data for easy retrieval. For your website, which offers Solr search, you may have thousands of indexes. These indexes might be saved locally or remotely. Sometimes the Solr Client is unable to connect to the remotely saved indexes. This could be for several reasons, for example, the indexes could be secured, or accessible only through urls. In such cases, we need to connect to Solr using standard PHP cURL.


Using cURL to Send Queries

cURL is simple to use, but not many prefer to use it. That is because they don’t know how specify the correct syntax for queries. To give you an idea, you can use the following queries. Even though these queries are specific to Solr, you can send any kind of data in CURLOPT_POSTFIELDS option.

[pre]// to index a document
$var= json_encode($documents);

* For Solr the details could be something like this:
* $protocol: https
* $port: 8983
* $host:
* $path: /solr/abcd
$url=”$protocol://”.$host.”:”.$port.$path.”/update/?commit=true” ;

$ch = curl_init();
$header = array(“Content-type:text/xml; charset=utf-8″);
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_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_USERPWD ,”$username:$password”);
curl_setopt($ch, CURLOPT_POSTFIELDS, $var);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);

$data = curl_exec($ch);

if (curl_errno($ch)) {
return 0 ;
else {
return $data;

The above example gives you an idea about indexing documents. Similarly if you wanted to delete data, you could use the following for $var:

[pre]// to delete data


Using cURL in PHP provides a definite option to retrieve data when all else fails. Try using this approach and let us know if it worked for you. You can direct any additional section to me using the comments section.

Aparna Gawade

Aparna Gawade

Leave a Reply

Your email address will not be published. Required fields are marked *

Get The Latest Updates

Subscribe to our Newsletter

A key to unlock the world of open-source. We promise not to spam your inbox.

Suggested Reads

Join our 55,000+ Subscribers

    The Wisdm Digest delivers all the latest news, and resources from the world of open-source businesses to your inbox.

    Suggested Reads