Skip to content
This repository has been archived by the owner on Jul 1, 2019. It is now read-only.

Commit

Permalink
Merge pull request #1 from swiftype/master
Browse files Browse the repository at this point in the history
Merged changes from mriley. Search and suggest are now using POST.
  • Loading branch information
Nevon committed Aug 14, 2012
2 parents b61e437 + b744117 commit 54c1529
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ A PHP client for [Swiftype](http://swiftype.com), a search and autocomplete API

$client = new \Swiftype\SwiftypeClient('your@email.com', 'password', 'api_key');

print_r($client->create_engine('Library'));
print_r($client->create_engine('library'));

print_r($client->create_document_type('Library', 'Books'));
print_r($client->create_document_type('library', 'books'));

print_r($client->create_document('Library', 'Books', array(
print_r($client->create_document('library', 'books', array(
'external_id' => '1',
'fields' => array(
array(
Expand All @@ -28,7 +28,7 @@ print_r($client->create_document('Library', 'Books', array(
)
)));

print_r($client->documents('Library', 'Books'));</pre>
print_r($client->documents('library', 'books'));</pre>

###Documentation

Expand All @@ -52,7 +52,7 @@ Returns a specific engine.
####create_engine(engine_id String)
Creates a new engine

`$client->create_engine('Library');`
`$client->create_engine('library');`

####destroy_engine(engine_id String)
Destroys an engine
Expand All @@ -72,7 +72,7 @@ Fetches a specific document_type.
####create_document_type(engine_id String, document_type_id String)
Creates a document type for a specific engine.

`$client->create_document_type('library', 'Books');`
`$client->create_document_type('library', 'books');`

####destroy_document_type(engine_id String, document_type_id String)
Destroys a document type.
Expand Down
28 changes: 16 additions & 12 deletions swiftype.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php
namespace Swiftype;

class SwiftypeClient {
Expand Down Expand Up @@ -102,25 +102,29 @@ public function destroy_documents($engine_id, $document_type_id, $document_ids =
public function search($engine_id, $document_type_id=null, $query, $options = array()) {
$query_string = array('q' => $query);
$full_query = array_merge($query_string, $options);
return $this->get($this->search_path($engine_id, $document_type_id), array(), $full_query);
return $this->post($this->search_path($engine_id, $document_type_id), array(), $full_query);
}

public function suggest($engine_id, $query, $options = array()) {
public function suggest($engine_id, $document_type_id=null, $query, $options = array()) {
$query_string = array('q' => $query);
$full_query = array_merge($query_string, $options);
return $this->get($this->suggest_path($engine_id), array(), $full_query);
return $this->post($this->suggest_path($engine_id, $document_type_id), array(), $full_query);
}

private function search_path($engine_id, $document_type_id = null) {
if ($document_type_id === null) {
return 'engines/'.$engine_id.'/search';
} else {
return 'engines/'.$engine_id.'/document_types/'.$document_type_id.'/documents';
return 'engines/'.$engine_id.'/document_types/'.$document_type_id.'/search';
}
}

private function suggest_path($engine_id) {
return 'engines/'.$engine_id.'/suggest';
private function suggest_path($engine_id, $document_type_id = null) {
if ($document_type_id === null) {
return 'engines/'.$engine_id.'/suggest';
} else {
return 'engines/'.$engine_id.'/document_types/'.$document_type_id.'/suggest';
}
}

private function engines_path() {
Expand Down Expand Up @@ -166,7 +170,7 @@ private function put($path, $params = array(), $data = array()) {
private function request($method, $path, $params = array(), $data = array()) {
//Final URL
$full_path = $this->host.$this->api_base_path.$path.'.json';

//Use the api key if we have it.
if ($this->api_key != null) {
$params['auth_token'] = $this->api_key;
Expand Down Expand Up @@ -209,14 +213,14 @@ private function request($method, $path, $params = array(), $data = array()) {

$response = curl_exec($request);
$error = curl_error($request);

if ($error) {
throw new \Exception("Sending message failed. Error: ". $error);
}

$http_status = (int)curl_getinfo($request,CURLINFO_HTTP_CODE);
curl_close($request);

//Any 2XX HTTP codes mean that the request worked
if (intval(floor($http_status / 100)) === 2) {
$final = json_decode($response);
Expand Down Expand Up @@ -263,4 +267,4 @@ private function request($method, $path, $params = array(), $data = array()) {
}
}

?>
?>

0 comments on commit 54c1529

Please sign in to comment.