Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An AJAX-Solr proxy written in PHP.
<?php
/**
* @file
* Implements a Solr proxy.
*
* Currently requires json_decode which is bundled with PHP >= 5.2.0.
*
* You must download the SolrPhpClient and store it in the same directory as this file.
*
* http://code.google.com/p/solr-php-client/
*/
require_once(dirname(__FILE__) . '/SolrPhpClient/Apache/Solr/Service.php');
solr_proxy_main();
/**
* Executes the Solr query and returns the JSON response.
*/
function solr_proxy_main() {
if (isset($_POST['query'])) {
$params = array();
$keys = '';
$core = '';
// The names of Solr parameters that may be specified multiple times.
$multivalue_keys = array('bf', 'bq', 'facet.date', 'facet.date.other', 'facet.field', 'facet.query', 'fq', 'pf', 'qf');
$pairs = explode('&', $_POST['query']);
foreach ($pairs as $pair) {
list($key, $value) = explode('=', $pair, 2);
$value = urldecode($value);
if (in_array($key, $multivalue_keys)) {
$params[$key][] = $value;
}
elseif ($key == 'q') {
$keys = $value;
}
elseif ($key == 'core') {
$core = "$value/";
}
else {
$params[$key] = $value;
}
}
// Replace these arguments as needed.
$solr = new Apache_Solr_Service('example.solrstuff.org', 80, '/solrjs/' . $core);
try {
$response = $solr->search($keys, $params['start'], $params['rows'], $params);
}
catch (Exception $e) {
die($e->__toString());
}
print $response->getRawResponse();
}
}
@marklreyes

This comment has been minimized.

Copy link

@marklreyes marklreyes commented Nov 28, 2013

Just curious on the finer details on integrating this php proxy. I've come across a roadblock. Could you evaluate my stackoverflow question and give me your thoughts?

http://stackoverflow.com/questions/20257418/proxy-php-not-working-w-ajax-solr

@guoxiangke

This comment has been minimized.

Copy link

@guoxiangke guoxiangke commented Dec 23, 2013

sorry,new to solr,and want to increase the Security
after download the solr-php-client and put right dir,
How to use this file?

@geomartino

This comment has been minimized.

Copy link

@geomartino geomartino commented Jan 23, 2014

solr don't seem to send to "start" parameter when it's equal to zero (0). Maybe we could verify that in the proxy and set the value if it's not present in the request?

https://gist.github.com/geomartino/8584403/revisions

thank you by the way for this piece of code,

MartinO

@nisha-kajale

This comment has been minimized.

Copy link

@nisha-kajale nisha-kajale commented Oct 15, 2015

HI,

i want to integrate ajax solr with php. Can anyone help me with the steps?
Thanks,
Nisha

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.