Created
July 12, 2018 12:24
-
-
Save paullinney/05e59092b23b013db7721ae9cdb5b68f to your computer and use it in GitHub Desktop.
Solr API Proxy - Run a request on a remote solr instance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Solr Query Proxy. | |
* - For use with Solarium Query Objects only. | |
*/ | |
error_reporting(0); | |
require __DIR__ . '/../vendor/autoload.php'; | |
// Config | |
$solr = [ | |
'endpoint' => [ | |
'localhost' => [ | |
'host' => 'solr.internal', | |
'port' => '8080', | |
'path' => '/solr/index', | |
], | |
], | |
]; | |
// Check the validity of the api key. | |
$headers = getallheaders(); | |
if(!isset($headers['Key'])) { | |
header('Content-Type: application/json'); | |
http_response_code(401); | |
print json_encode(['message' => 'Must provide API key as header.']); exit; | |
} | |
if($headers['Key'] !== 'APIKEY') { // replace with a better API key | |
header('Content-Type: application/json'); | |
http_response_code(401); | |
print json_encode(['message' => 'API key is invalid.']); exit; | |
} | |
// Grab the body of the request and serialize the body. | |
$entityBody = file_get_contents('php://input'); | |
$query = unserialize($entityBody); | |
// Check we have a valid Solarium Query Object, if not throw an error. | |
if(!is_a($query, 'Solarium\QueryType\Select\Query\Query')) { | |
header('Content-Type: application/json'); | |
http_response_code(422); | |
print json_encode([ | |
'message' => 'Must be an instance of "Solarium\QueryType\Select\Query\Query"' | |
]); | |
exit; | |
} | |
// Create the query response | |
$client = new \Solarium\Client($solr); | |
$request = $client->createRequest($query); | |
$response = $client->executeRequest($request); | |
$result_set_transport = serialize($response); | |
print $result_set_transport; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment