forked from simde-utc/ginger-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKoalaClient.class.php
More file actions
60 lines (51 loc) · 1.53 KB
/
KoalaClient.class.php
File metadata and controls
60 lines (51 loc) · 1.53 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
<?php
require_once 'ApiException.class.php';
class KoalaClient {
protected $url = "";
protected $useragent = "KoalaClient/0.1";
protected function apiCall($endpoint, $params = array(), $method = "GET") {
// Construction de la chaîne de paramètres
$paramstring = "";
if (!empty($params)) {
foreach ($params as $key => $param) {
$paramstring .= $key . "=" . $param . "&";
}
// On supprimer le dernier &
$paramstring = substr($paramstring, 0, -1);
}
// Réglages de cURL
$settings = array(
CURLOPT_USERAGENT => $this->useragent,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_CAINFO => __DIR__."/TERENA_SSL_CA.pem",
);
// Construction de l'URL et des postfields
if($method == "GET"){
$url = $this->url . $endpoint . "?" . $paramstring;
}
else {
$url = $this->url . $endpoint;
$settings[CURLOPT_POSTFIELDS] = $params;
}
// Initialisation de cURL
$ch = curl_init($url);
curl_setopt_array($ch, $settings);
// Éxécution de la requête
$result = curl_exec($ch);
// Si erreur d'appel de cron
if (curl_errno($ch) != 0) {
throw new ApiException(503);
}
// Si erreur http, on la renvoie
else if (curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200) {
throw new ApiException(curl_getinfo($ch, CURLINFO_HTTP_CODE));
}
// Sinon, on renvoie les infos
else {
return json_decode($result);
}
}
}
?>