emis/php-api-client

EMIS PHP API Client Library

1.3.11 2019-09-16 14:52 UTC

README

EMIS has a rich news and reports content from thousands of renowned local and global sources. EMIS Documents API allows you to access the news and reports we have through your own integrated database. We provide you the data in XML format which you can either download in mass quantities (feed) or by on demand searches (API). EMIS API PHP SDK provides an easy way to quickly access the needed information in an object oriented way with minimum knowledge about the API.

composer.json

Create a new project and add the EMIS API composer dependency:

composer require emis/php-api-client

Autoload

In your script, initialize the composer's autoloader:

require('vendor/autoload.php');

Initialization and login/logout

As a minimum your script should login/logout to EMIS API:

use Emis\Http\Client;

$client = new Client();
$client->setApi(Client::DOCUMENTS_API);
$client->login('username', 'password');

$client->logout();

Reusing sessions

It's much better if your script handles a permanent session rather than login/logout on each request. You should store the generated by the login session token and then reuse it on next requests. Logout should not be performed in this case as it will destroy the current session and you won't be able to reuse the token. Tokens are expiring on daily basis. Below you can find an example that stores the current session in the filesystem:

$token = file_get_contents('token');
$client = new Client();
$client->setApi(Client::DOCUMENTS_API);

if (!$token) {
    $client->login('username', 'password');
    file_put_contents('token', $client->getToken());
} else {
    $client->setToken($token);
}

Sending API Requests

API requests are made through a set of proxy classes you could find under Emis\Document\Api\Proxy namespace. A simple example getting document titles for country code 'HU':

use Emis\Document\Api\Proxy\Search;

...

$search = new Search($client);
$result = $search->query('HU');

foreach ($result->getDocuments() as $document) {
    echo $document->getTitle() . "\n";
}

Additional Examples

Using searchQuery method to easily pass the needed query parameters

use Emis\Entity\Api\Document\Search as SearchRequest;

...

$search = new Search($client);
$searchRequest = new SearchRequest();
$searchRequest->setCountries(array('RU'));
$searchRequest->setLanguages(array('en', 'ru'));
$result = $search->searchQuery($searchRequest);

$counter = array('en' => 0, 'ru' => 0);

foreach ($result->getDocuments() as $document) {
    $counter[$document->getLanguage()->getCode()]++;
}

printf("Total of %s en documents found.\n", $counter['en']);
printf("Total of %s ru documents found.\n", $counter['ru']);

Using Search::getDocuments as a sub-request to get the documents one by one

$search = new Search($client);
$result = $search->query('HU');

foreach ($result->getDocuments() as $document) {
    $subResult = $search->getDocuments($document->getId());

    foreach ($subResult->getDocuments() as $subDocument) {
        echo $subDocument->getTitle() . "\n";
    }
}

Get list of dictionary entities (countries)

The example code prints the country code and country name, and can be easily modify to return results for any other Dictionary method.

use Emis\Document\Api\Proxy\Dictionary;

...

$dictionary = new Dictionary($client);

foreach ($dictionary->getCountries() as $country) {
    echo $country->getCode() . " : " . $country->getName() . "\n";
}

Usage tracking for single document

use Emis\Document\Api\Proxy\Search;
use Emis\Document\Api\Proxy\Usage;

...

$search = new Search($client);
$result = $search->query('HU');
$documents = $result->getDocuments();

$usage = new Usage($client);
$usage->report($documents[0]->getId(), new \DateTime());

Usage tracking for multiple documents

use Emis\Document\Api\Proxy\Search;
use Emis\Document\Api\Proxy\Usage;
use Emis\Entity\Api\UsageReport\Document as ReportDocument;
use Emis\Entity\Api\UsageReport\Request as UsageRequest;

... 

$search = new Search($client);
$result = $search->query('HU');

$usage = new Usage($client);
$request = new UsageRequest();

foreach ($result->getDocuments() as $document) {
    $reportDocument = new ReportDocument();
    $reportDocument->setDocumentId($document->getId());
    $reportDocument->setAccessTime(new \DateTime());
    $request->addDocument($reportDocument);
}

$usage->reportMultiple($request);