1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class ApiCallService |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace HDNET\OnpageIntegration\Service; |
7
|
|
|
|
8
|
|
|
use HDNET\OnpageIntegration\Exception\UnavailableException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class ApiCallService |
12
|
|
|
*/ |
13
|
|
|
class ApiCallService |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
private $url = 'https://api.onpage.org/zoom/json'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Start the api call |
20
|
|
|
* |
21
|
|
|
* @param string $json |
22
|
|
|
* |
23
|
|
|
* @return string |
24
|
|
|
*/ |
25
|
|
|
public function makeCall($json) |
26
|
|
|
{ |
27
|
|
|
try { |
28
|
|
|
$this->checkForCurl(); |
29
|
|
|
|
30
|
|
|
return $this->send($json); |
31
|
|
|
} catch (UnavailableException $e) { |
|
|
|
|
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Check if curl is loaded |
37
|
|
|
* |
38
|
|
|
* @throws \Exception |
39
|
|
|
*/ |
40
|
|
|
protected function checkForCurl() |
41
|
|
|
{ |
42
|
|
|
if (!extension_loaded('curl')) { |
43
|
|
|
throw new \Exception('The curl extension needs to be enabled.'); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Send the api request |
49
|
|
|
* |
50
|
|
|
* @param string $data |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
* @throws UnavailableException |
54
|
|
|
*/ |
55
|
|
|
protected function send($data) |
56
|
|
|
{ |
57
|
|
|
$ch = curl_init(); |
58
|
|
|
curl_setopt($ch, CURLOPT_URL, $this->url); |
59
|
|
|
curl_setopt($ch, CURLOPT_HEADER, 0); |
60
|
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); |
61
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 10); |
62
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
63
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); |
64
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); |
65
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array( |
66
|
|
|
'Content-Type: application/json', |
67
|
|
|
'Content-Length: ' . strlen($data) |
68
|
|
|
)); |
69
|
|
|
$return = curl_exec($ch); |
70
|
|
|
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
71
|
|
|
if ($code >= 500) { |
72
|
|
|
throw new UnavailableException('The API could not be reached.'); |
73
|
|
|
} |
74
|
|
|
if ($code >= 400) { |
75
|
|
|
throw new UnavailableException('There has been an error reaching the API.'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $return; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|