Skip to content

Instantly share code, notes, and snippets.

@phillro
Created August 15, 2011 17:50
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phillro/1147306 to your computer and use it in GitHub Desktop.
Save phillro/1147306 to your computer and use it in GitHub Desktop.
Elastica with an authenticated search
<?php
set_include_path(get_include_path() . PATH_SEPARATOR . realpath(dirname(__FILE__) . '/Elastica/lib'));
function elasticsearch_autoload($class) {
$file = str_replace('_', '/', $class) . '.php';
require_once $file;
}
spl_autoload_register('elasticsearch_autoload');
defined('BASE_PATH') || define('BASE_PATH', realpath(dirname(__FILE__)));
$authHeaderValue = 'Basic '.encodeAuth('YOURUSERNAME', 'YOURPASSWORD').'==';
$authHeader = array('Authorization'=>$authHeaderValue);
$config = array(
'host' => 'esh01.elasticsearchhq.com',
'port' => '9200',
'transport' => 'https',
'headers' => $authHeader
);
$client = new Elastica_Client($config);
$index = $client->getIndex('YOURINDEXNAME');
$resultSet = $index->search('YOUR QUERY');
echo var_export($resultSet, true);
function encodeAuth($userName, $password){
$encodedAuth = base64_encode($userName.':'.$password);
return $encodedAuth;
}
?>
@Artem-Schander
Copy link

Artem-Schander commented Jan 13, 2017

For me it works only without the .'==' in this line:
$authHeaderValue = 'Basic '.encodeAuth('YOURUSERNAME', 'YOURPASSWORD').'==';

Edit
I've just looked it up and PHP adds that equal signs by itself. Doku

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment