· 6 years ago · Feb 07, 2020, 12:44 PM
1class API
2{
3 # Cliniko
4 private $headers;
5 private $cSessions;
6 private $cMulti;
7 private $result;
8
9 # MindBody
10 private $staffUserToken;
11 private $siteId;
12 private $appName;
13 private $mKey;
14
15 # Cliniko
16 public function prepareForGet()
17 {
18 $this->createExtraHeaders();
19 $this->cSessions = array();
20 $this->cMulti = curl_multi_init();
21 }
22
23 public function addToGET($url)
24 {
25 GLOBAL $agent;
26
27 $cSingle = curl_init();
28 curl_setopt($cSingle, CURLOPT_URL, $url);
29 curl_setopt($cSingle, CURLOPT_RETURNTRANSFER, true);
30 curl_setopt($cSingle, CURLOPT_USERAGENT, $agent);
31 curl_setopt($cSingle, CURLOPT_HTTPHEADER, $this->headers);
32
33 array_push($this->cSessions, $cSingle);
34 curl_multi_add_handle($this->cMulti, $cSingle);
35 }
36
37 private function createExtraHeaders()
38 {
39 GLOBAL $key;
40 $this->headers = [
41 "Authorization: Basic " . base64_encode($key),
42 "Accept: application/json"
43 ];
44 }
45
46 # MindBody
47 private function mCreateExtraHeaders()
48 {
49 $this->headers = [
50 "authorization: " . $this->staffUserToken,
51 "Api-Key: " . $this->mKey,
52 "SiteId: " . $this->siteId,
53 "Accept: application/json"
54 ];
55 }
56
57 public function mPrepareForGet(){
58 $this->mCreateExtraHeaders();
59 $this->cSessions = array();
60 $this->cMulti = curl_multi_init();
61 }
62
63 public function mAddToGET($url)
64 {
65
66 $cSingle = curl_init();
67 curl_setopt($cSingle, CURLOPT_URL, $url);
68 curl_setopt($cSingle, CURLOPT_RETURNTRANSFER, true);
69 curl_setopt($cSingle, CURLOPT_USERAGENT, $this->appName);
70 curl_setopt($cSingle, CURLOPT_HTTPHEADER, $this->headers);
71
72 array_push($this->cSessions, $cSingle);
73 curl_multi_add_handle($this->cMulti, $cSingle);
74 }
75
76 # General
77 public function executeGET()
78 {
79 $status = null;
80
81 //execute the multi handle
82 do {
83 $status = curl_multi_exec($this->cMulti, $active);
84 if ($active) {
85 // Wait a short time for more activity
86 curl_multi_select($this->cMulti);
87 }
88 } while ($active && $status == CURLM_OK);
89
90 $this->result = array();
91
92 foreach ($this->cSessions as $cSession) {
93 $singleResponse = json_decode(curl_multi_getcontent($cSession));
94 array_push($this->result, $singleResponse);
95 curl_multi_remove_handle($this->cMulti, $cSession);
96 }
97
98 // Reset
99 curl_multi_close($this->cMulti);
100 $this->cMulti = null;
101 $this->cSessions = array();
102 }
103
104 public function getResult()
105 {
106 return $this->result;
107 }
108
109 # Setters
110 public function setStaffUserToken($staffUserToken)
111 {
112 $this->staffUserToken = $staffUserToken;
113 }
114
115 public function setSiteId($siteId)
116 {
117 $this->siteId = $siteId;
118 }
119
120 public function setAppName($appName)
121 {
122 $this->appName = $appName;
123 }
124
125 public function setMKey($mKey)
126 {
127 $this->mKey = $mKey;
128 }
129
130
131}