This repository was archived by the owner on Jun 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemvi.php
More file actions
110 lines (92 loc) · 2.64 KB
/
emvi.php
File metadata and controls
110 lines (92 loc) · 2.64 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
class EmviClientConfig {
private $authHost = "https://auth.emvi.com";
private $apiHost = "https://api.emvi.com";
function setAuthHost($authHost) {
$this->authHost = $authHost;
}
function getAuthHost() {
return $this->authHost;
}
function setAPIHost($apiHost) {
$this->apiHost = $apiHost;
}
function getAPIHost() {
return $this->apiHost;
}
}
class EmviClient {
private $clientId;
private $clientSecret;
private $organization;
private $config;
function __construct($clientId, $clientSecret, $organization, $config = null) {
$this->clientId = $clientId;
$this->clientSecret = $clientSecret;
$this->organization = $organization;
if(!is_null($config)) {
$this->config = $config;
}
else {
$this->config = new EmviClientConfig();
}
}
function refreshToken() {
$url = $this->config->getAuthHost()."/api/v1/auth/token";
$data = array(
"grant_type" => "client_credentials",
"client_id" => $this->clientId,
"client_secret" => $this->clientSecret
);
$options = array(
"http" => array(
"method" => "POST",
"header" => "Content-Type: application/x-www-form-urlencoded\r\n",
"content" => json_encode($data)
)
);
$context = stream_context_create($options);
$result = @file_get_contents($url, false, $context);
if($result === FALSE) {
throw new Exception("Error refreshing token: ".$http_response_header[0]);
}
$resp = json_decode($result);
$_SESSION["emvi_access_token"] = $resp->access_token;
}
function getArticle($id, $langId = "", $version = 0, $retry = true) {
$url = $this->config->getAPIHost()."/api/v1/article/".$id."?lang=".$langId."&version=".$version;
$options = array(
"http" => array(
"method" => "GET",
"header" => $this->getHeader()
)
);
$context = stream_context_create($options);
$result = @file_get_contents($url, false, $context);
if($result === FALSE) {
$responseHeader = $http_response_header[0];
if($this->isUnauthorized($responseHeader) && $retry) {
$this->refreshToken();
return $this->getArticle($id, $langId, $version, false);
}
else {
throw new Exception("Error reading article: ".$responseHeader." URL: ".$url);
}
}
return json_decode($result);
}
private function getHeader() {
$token = "";
if(isset($_SESSION["emvi_access_token"])) {
$token = $_SESSION["emvi_access_token"];
}
return "Authorization: Bearer ".$token."\r\n".
"Organization: ".$this->organization."\r\n".
"Client: ".$this->clientId."\r\n".
"Content-Type: application/json\r\n";
}
private function isUnauthorized($header) {
return strpos($header, "401") !== FALSE;
}
}
?>