Technology WordPress Tips & Tricks

Using PHP cURL to Send Queries to Solr

Connecting to Solr from PHP takes a little care to get right. Here is how to use PHP cURL to send queries to Solr, so your custom search integration talks to the server reliably instead of timing out.

Aparna Gawade Aparna Gawade 2 min read
Using PHP cURL to Send Queries to Solr

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.

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: abc-index.solrdata.com
* $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_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_USERPWD ,”$username:$password”);
curl_setopt($ch, CURLOPT_POSTFIELDS, $var);
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)) {
return 0 ;
}
else {
return $data;
}
curl_close($ch);[/pre]

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
$var='<delete><query>id:*</query></delete>’;[/pre]

 

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.

Get a FREE Consultation

Let's build something that lasts.

Share what's on your mind — a clear brief, a half-formed idea, or just a sense that something needs to change. We'll listen first, ask the right questions, and point you toward what's actually worth building.

We take on a handful of projects each quarter,ones where we can truly make a difference.

  • Receive a human response within 24 hours
  • Get a detailed scope and quote upfront
  • We're happy to sign an NDA upon request

    Free 30-Min Strategy Call

    Your Name *

    Your Phone No *

    Work Email *

    Your Budget*

    Project Details *