-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathopencalais.php
80 lines (67 loc) · 2.34 KB
/
opencalais.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
/**
* Open Calais Tags
*
* @version: 2.0
* @author: Dan Grossman http://www.dangrossman.info/
* @copyright: Copyright (c) 2012-2015 Dan Grossman. All rights reserved.
* @license: Licensed under the MIT license. See http://www.opensource.org/licenses/mit-license.php
*
*/
class OpenCalaisException extends Exception {}
/**
* Class OpenCalais. Working with OpenCalais API
*/
class OpenCalais {
public $outputFormat = 'application/json';
public $contentType = 'text/html';
private $api_url = 'https://api.thomsonreuters.com/permid/calais';
private $api_token = '';
private $entities = array();
/**
* @param string $api_token
* @throws OpenCalaisException
*/
public function __construct($api_token) {
if (empty($api_token)) {
throw new OpenCalaisException('An OpenCalais API token is required to use this class.');
}
$this->api_token = $api_token;
}
/**
* Return entities by document
* @param string $document
* @return array
* @throws OpenCalaisException
*/
public function getEntities($document) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->api_url);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array(
'X-AG-Access-Token: ' . $this->api_token,
'Content-Type: ' . $this->contentType,
'outputFormat: ' . $this->outputFormat
)
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $document);
$response = curl_exec($ch);
$response = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $response);
$object = json_decode($response);
if (empty($object)) {
throw new OpenCalaisException('No response was received from the API.');
} elseif (isset($object->fault)) {
throw new OpenCalaisException('OpenCalais Error:' . $object->fault->faultstring);
}
foreach ($object as $item) {
if (!empty($item->_typeGroup) && !empty($item->name)) {
$this->entities[$item->_typeGroup][] = trim($item->name);
}
}
return $this->entities;
}
}