Skip to content

Commit

Permalink
Merge pull request #8 from doofinder/2396-php-client-auth
Browse files Browse the repository at this point in the history
2396 php client auth
  • Loading branch information
ecoslado committed Mar 31, 2015
2 parents 7f3e100 + 8a20019 commit 2bd27f8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ Version 4.
- CamelCase convention for everything. "has_next(0" is now "hasNext()"
- And empty query() prompts a "match all" query
- Facets/Filtering support

Version 5.
----------
- API Key Authorization. API Key required in client constructor.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ Quick & Dirty
* Instantiate the object

````php
<?php $df = new DoofinderApi('6a9abc4dc17351123b1e0198af92e6e9'); // specify hashid ?>
<?php $df = new DoofinderApi('6a9abc4dc17351123b1e0198af92e6e9',
'eu1-384fd8a73c7ff0859a5891f9f4083b1b9727f9c3'); // specify hashid and API KEY ?>
````

* If you feel like it, you can specify filters.
Expand Down Expand Up @@ -216,7 +217,8 @@ you can use it to build links to searh results:

````php
<?php
$df = DoofinderApi('6a9abc4dc17351123b1e0198af92e6e9');
$df = new DoofinderApi('6a9abc4dc17351123b1e0198af92e6e9',
'eu1-384fd8a73c7ff0859a5891f9f4083b1b9727f9c3');
$df->fromQuerystring(); // get search string, pagenum, rpp, etc from the request
$dfResults = $df->query(); // no need to specify query or page, it's already set through the 'fromQuerystring' method
````
Expand All @@ -225,7 +227,8 @@ Also , the second arg in constructor has the same effect. This code is equivalen

````php
<?php
$df = DoofinderApi('6a9abc4dc17351123b1e0198af92e6e9',
$df = new DoofinderApi('6a9abc4dc17351123b1e0198af92e6e9',
'eu1-384fd8a73c7ff0859a5891f9f4083b1b9727f9c3',
true // call "fromQuerystring" when initializing
);
$dfResults = $df->query();
Expand Down Expand Up @@ -254,7 +257,8 @@ When specifying filters in request parameters, follow this convention:

````php
<?php
$df = DoofinderApi('6a9abc4dc17351123b1e0198af92e6e9', // hashid
$df = new DoofinderApi('6a9abc4dc17351123b1e0198af92e6e9', //hashid
'eu1-384fd8a73c7ff0859a5891f9f4083b1b9727f9c3', // api_key
true, // get params from request
array(
'prefix' => 'sp_df_df_', // prefix to use with toQuerystring. CAN'T USE EMPTY STRINGS
Expand All @@ -267,7 +271,8 @@ $df = DoofinderApi('6a9abc4dc17351123b1e0198af92e6e9', // hashid
#### Defaults ####
````php
<?php
$df = DoofinderApi('6a9abc4dc17351123b1e0198af92e6e9', // hashid
$df = new DoofinderApi('6a9abc4dc17351123b1e0198af92e6e9' //hashid
'eu1-384fd8a73c7ff0859a5891f9f4083b1b9727f9c3', // api_key
false, // don't obtain status from request
array(
'prefix' => 'dfParam_',
Expand Down
34 changes: 28 additions & 6 deletions lib/doofinder_api.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,26 @@
* under the License.
*/


class DoofinderApi{
/*
* Basic client for an account.
* It needs an API url to be constructed.
* Its only method is to query the doofinder search server
* Returns a DoofinderResults object
*/
const url = 'http://eu1-search.doofinder.com';

const URL_SUFFIX = '-search.doofinder.com';
const DEFAULT_TIMEOUT = 10000;
const DEFAULT_RPP = 10;
const DEFAULT_PARAMS_PREFIX = 'dfParam_';
const DEFAULT_API_VERSION = '4';

private $api_key = null; // user API_KEY
private $hashid = null; // hashid of the doofinder account

private $apiVersion = null;
private $apiVersion = null;
private $url = null;
private $results = null;
private $query = null;
private $search_options = array(); // assoc. array with doofinder options to be sent as request parameters
Expand All @@ -56,7 +61,18 @@ class DoofinderApi{
* to look for params when unserializing. either 'get' or 'post'
* @throws DoofinderException if $hashid is not a md5 hash or api is no 4, 3.0 or 1.0
*/
function __construct($hashid, $fromParams=false, $init_options = array()){
function __construct($hashid, $api_key, $fromParams=false, $init_options = array()){

$zone_key_array = explode('-', $api_key);

if(2 === count($zone_key_array)){
$this->api_key = $zone_key_array[1];
$this->zone = $zone_key_array[0];
$this->url = "http://" . $this->zone . self::URL_SUFFIX;
} else {
throw new DoofinderException("API Key is no properly set.");
}

if(array_key_exists('prefix', $init_options)){
if($init_options['prefix'] != ''){
$this->paramsPrefix = $init_options['prefix'];
Expand Down Expand Up @@ -95,17 +111,23 @@ function __construct($hashid, $fromParams=false, $init_options = array()){
}

}


private function reqHeaders(){
$headers = array();
$headers[] = 'Expect:'; //Fixes the HTTP/1.1 417 Expectation Failed
$headers[] = "API Token: " . $this->api_key; //API Authorization
return $headers;
}

private function apiCall($params){
$params['hashid'] = $this->hashid;
$args = http_build_query(array_filter($params)); // remove any null value from the array
$url = self::url.'/'.$this->apiVersion.'/search?'.$args;
$url = $this -> url . '/' . $this->apiVersion . '/search?' . $args;
$session = curl_init($url);
curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($session, CURLOPT_HEADER, false); // Tell curl not to return headers
curl_setopt($session, CURLOPT_RETURNTRANSFER, true); // Tell curl to return the response
curl_setopt($session, CURLOPT_HTTPHEADER, array('Expect:')); //Fixes the HTTP/1.1 417 Expectation Failed
curl_setopt($session, CURLOPT_HTTPHEADER, $this->reqHeaders()); // Adding request headers
$response = curl_exec($session);
$httpCode = curl_getinfo($session, CURLINFO_HTTP_CODE);
curl_close($session);
Expand Down

0 comments on commit 2bd27f8

Please sign in to comment.