Created
August 15, 2011 17:50
-
-
Save phillro/1147306 to your computer and use it in GitHub Desktop.
Elastica with an authenticated search
This file contains hidden or 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 | |
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; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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