HODClient Library for PHP
Official PHP client library to help with calling Haven OnDemand APIs., (*1)
What is Haven OnDemand?
Haven OnDemand is a set of over 70 APIs for handling all sorts of unstructured data. Here are just some of our APIs' capabilities:
* Speech to text
* OCR
* Text extraction
* Indexing documents
* Smart search
* Language identification
* Concept extraction
* Sentiment analysis
* Web crawlers
* Machine learning, (*2)
For a full list of all the APIs and to try them out, check out https://www.havenondemand.com/developer/apis, (*3)
Integrate HODClient into php project
Download from Packagist and include in app
Run the following command from your terminal (composer must be installed), (*4)
composer require havenondemand/havenondemand
Place the following line in your app to include the library, (*5)
include './vendor/havenondemand/havenondemand/lib/hodclient.php';
include './vendor/havenondemand/havenondemand/lib/hodresponseparser.php';
Download directly from Github
- Download the HODClient and HODResponseParser libraries for PHP.
- Unzip the file and copy the hodclient.php and hodresponseparser.php under the lib folder to your project folder.
Using the library
Creates and initializes a HODClient object., (*6)
HODClient($apiKey, $version = "v1")
-
$apiKey
is your developer apikey.
-
$version
Haven OnDemand API version. The default value is "v1".
Example code:, (*7)
include "hodclient.php"
$hodClient = new HODClient("API_KEY");
If you want to change the API version without the need to recreate the instance of the HOD client., (*8)
SetVersion($newversion)
-
$newVersion
a string to specify an API version as "v1" or "v2"
If you want to change the API_KEY without the need to recreate the instance of the HOD client., (*9)
SetAPIKey($newApiKey)
-
newApiKey
a string to specify a new API_KEY
#
Function GetRequest, (*10)
Sends a HTTP GET request to call an Haven OnDemand API., (*11)
GetRequest($paramArr, $hodApp, $async, $callback)
-
$paramArr
is an array() containing key/value pair parameters to be sent to a Haven OnDemand API, where the keys are the parameters of that Haven OnDemand API.
Note: If a parameter type is an array<>, the value must be defined as an array() or []., (*12)
E.g.:, (*13)
$sources = array();
array_push($sources, "http://www.cnn.com");
array_push($sources, "http://www.bbc.com");
$paramArr = array(
'url' => $sources,
'entity_type' => ["people_eng","places_eng","companies_eng"]
);
-
$hodApp
is a string to identify a Haven OnDemand API. E.g. "extractentities".
-
$async [true | false]
specifies API call as Asynchronous or Synchronous.
-
$callback
the name of a callback function. If the $callback is omitted, or is an empty string "", this function will return a response.
Example code:, (*14)
// Call the Entity Extraction API synchronously to find people, places and companies from CNN website.
$paramArr = array(
'url' => "http://www.cnn.com",
'entity_type' => ["people_eng","places_eng","companies_eng"]
);
$response = GetRequest($paramArr, HODApps::ENTITY_EXTRACTION, false);
#
Function PostRequest, (*15)
Sends a HTTP POST request to call a Haven OnDemand API., (*16)
PostRequest($paramArr, $hodApp, $async, $callback)
-
$paramArr
is an array() containing key/value pair parameters to be sent to a Haven OnDemand API, where the keys are the parameters of that Haven OnDemand API.
Note: If a parameter type is an array<>, the value must be defined as an array() or []., (*17)
E.g.:, (*18)
$sources = array();
array_push($sources, "http://www.cnn.com");
array_push($sources, "http://www.bbc.com");
$paramArr = array(
'url' => $sources,
'entity_type' => ["people_eng","places_eng","companies_eng"]
);
-
$hodApp
is a string to identify an Haven OnDemand API. E.g. "ocrdocument".
-
$async [true | false]
specifies API call as Asynchronous or Synchronous.
-
$callback
the name of a callback function. If the $callback is omitted, or is an empty string "", this function will return a response.
Example code:, (*19)
// Call the OCR Document API asynchronously to scan text from an image file.
$paramArr = array(
'file' => "full/path/filename.jpg",
'mode' => "document_photo")
);
$response = $hodClient->PostRequest($paramArr, HODApps::OCR_DOCUMENT, true);
#
Function GetJobResult, (*20)
Sends a request to Haven OnDemand to retrieve content identified by a job ID., (*21)
GetJobResult($jobID, $callback)
-
$jobID
the job ID returned from an Haven OnDemand API upon an asynchronous call.
-
$callback
the name of a callback function, which the HODClient will call back and pass the response from server. If the $callback is omitted, or is an empty string "", this function will return a response.
#, (*22)
Function GetJobStatus, (*23)
Sends a request to Haven OnDemand to retrieve the status of a job identified by a job ID., (*24)
GetJobStatus($jobID, $callback)
-
$jobID
the job ID returned from an Haven OnDemand API upon an asynchronous call.
-
$callback
the name of a callback function, which the HODClient will call back and pass the response from server. If the $callback is omitted, or is an empty string "", this function will return a response.
#
Function GetRequestCombination, (*25)
Sends a HTTP GET request to call a combination API., (*26)
GetRequestCombination($paramArr, $hodApp, $async, $callback)
-
$paramArr
is an array() containing key/value pair parameters to be sent to a Haven OnDemand API, where the keys are the parameters of the calling API.
Note: If a parameter type is an array [] or a JSON object {}, the value must be quoted as a string.
E.g.:, (*27)
$paramArr = array(
'url' => 'http://www.bbc.com',
'entity_type' => '["people_eng","places_eng","companies_eng"]'
);
-
$hodApp
is the name of the combination API you are calling
-
$async [true | false]
specifies API call as Asynchronous or Synchronous.
-
$callback
the name of a callback function. If the $callback is omitted, or is an empty string "", this function will return a response.
Example code:, (*28)
// Call the Entity Extraction API synchronously to find people, places and companies from CNN website.
$paramArr = array(
'url' => "http://www.cnn.com",
'entity_type' => '["people_eng","places_eng","companies_eng"]'
);
$response = GetRequestCombination($paramArr, "combination_api_name", false);
#
Function PostRequestCombination, (*29)
Sends a HTTP POST request to call a combination API., (*30)
PostRequestCombination($paramArr, $hodApp, $async, $callback)
-
$paramArr
is an array() containing key/value pair parameters to be sent to a Haven OnDemand API, where the keys are the parameters of the calling API.
Note: If a parameter type is an array [] or a JSON object {}, the value must be quoted as a string.
E.g.:, (*31)
$paramArr = array(
'url' => 'http://www.bbc.com',
'entity_type' => '["people_eng","places_eng","companies_eng"]'
);
-
$hodApp
is the name of the combination API you are calling
-
$async [true | false]
specifies API call as Asynchronous or Synchronous.
-
$callback
the name of a callback function. If the $callback is omitted, or is an empty string "", this function will return a response.
Example code:, (*32)
// Call the Entity Extraction API synchronously to find people, places and companies from CNN website.
$paramArr = array(
'url' => "http://www.cnn.com",
'entity_type' => '["people_eng","places_eng","companies_eng"]'
);
$response = PostRequestCombination($paramArr, "combination_api_name", false);
#, (*33)
Demo code 1:
Call the Entity Extraction API to extract people and places from cnn.com website with a synchronous GET request, (*34)
entities;
for ($i = 0; $i type == "people_eng") {
$people .= $entity->normalized_text . ";
// parse any other interested information about this person ...
} else if ($entity->type == "places_eng") {
$places .= $entity->normalized_text . ";
// parse any other interested information about this place ...
}
}
echo "PEOPLE: " . $people;
echo "
echo "PLACES: " . $places;
}
$hodClient = new HODClient("YOUR-API-KEY");
$paramArr = array(
'url' => "http://www.cnn.com",
'entity_type' => ["people_eng","places_eng"]
);
try {
$hodClient->GetRequest($paramArr, HODApps::ENTITY_EXTRACTION, false, 'requestCompletedWithContent');
} catch (Exception $ex) {
echo $ex->getMessage();
}
?>
Demo code 2:
Call the OCR Document API to scan text from an image with an asynchronous POST request, (*35)
error != null) {
echo (json_encode($resp->error));
}else {
$hodClient->GetJobStatus($resp->jobID, 'requestCompletedWithContent');
}
}
// implement callback function
function requestCompletedWithContent($response) {
$resp = new HODResponseParser($response);
if ($resp->error != null){
$err = $resp->error;
if ($err->error == HODErrorCode::QUEUED) {
sleep(2);
$hodClient->GetJobStatus($err->jobID, 'requestCompletedWithContent');
} else if ($err->error == HODErrorCode::IN_PROGRESS) {
sleep(5);
$hodClient->GetJobStatus($err->jobID, 'requestCompletedWithContent');
} else {
$result = "Error:";
$result .= $resp->error->error . "";
$result .= $resp->error->reason . "";
}
}
else {
$result = "";
$textBlocks = $response->text_block;
for ($i = 0; $i ";
$result .= preg_replace("/\n+/", "", $block->text);
$result .= ", (*36)
";
}
}
echo "RECOGNIZED TEXT: ".$result;
}
$hodClient = new HODClient("YOUR-API-KEY");
$paramArr = array(
'url' => "https://www.hodondemand.com/sample-content/images/speccoll.jpg",
'mode' => "document_photo"
);
try {
$hodClient->PostRequest($paramArr, HODApps::OCR_DOCUMENT, true, 'requestCompletedWithJobId');
} catch (Exception $ex) {
echo $ex->getMessage();
}
?>
, (*37)
License
Licensed under the MIT License., (*38)