Skip to content

Commit 05fbc4a

Browse files
committed
Added our doCall method, which allows us to make make generic calls to the API
1 parent 98d8e70 commit 05fbc4a

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

Api.php

+80
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,86 @@ public function __construct($url, $email = null, $apiKey = null)
6262
}
6363
}
6464

65+
/**
66+
* Make the call
67+
*
68+
* @param string $method The method to call.
69+
* @param array $parameters The parameters to pass.
70+
* @param string $httpMethod The HTTP method to use.
71+
* @param bool $authenticate Should we use authentication?
72+
* @return mixed
73+
*/
74+
public function doCall(
75+
$method,
76+
array $parameters = null,
77+
$httpMethod = 'GET',
78+
$authenticate = true
79+
) {
80+
// build the url
81+
$url = $this->getUrl();
82+
83+
$parameters['method'] = (string) $method;
84+
$parameters['format'] = 'json';
85+
86+
// HTTP method
87+
if ($httpMethod == 'POST') {
88+
$options[CURLOPT_POST] = true;
89+
$options[CURLOPT_POSTFIELDS] = http_build_query($parameters);
90+
} else {
91+
$options[CURLOPT_POST] = false;
92+
if (!empty($parameters)) {
93+
$url .= '?' . http_build_query($parameters);
94+
}
95+
}
96+
97+
// set options
98+
$options[CURLOPT_URL] = $url;
99+
$options[CURLOPT_USERAGENT] = $this->getUserAgent();
100+
if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) {
101+
$options[CURLOPT_FOLLOWLOCATION] = true;
102+
}
103+
$options[CURLOPT_RETURNTRANSFER] = true;
104+
$options[CURLOPT_TIMEOUT] = (int) $this->getTimeOut();
105+
$options[CURLOPT_SSL_VERIFYPEER] = false;
106+
$options[CURLOPT_SSL_VERIFYHOST] = false;
107+
108+
// init
109+
$curl = curl_init();
110+
111+
// set options
112+
curl_setopt_array($curl, $options);
113+
114+
// execute
115+
$response = curl_exec($curl);
116+
$headers = curl_getinfo($curl);
117+
118+
// fetch errors
119+
$errorNumber = curl_errno($curl);
120+
$errorMessage = curl_error($curl);
121+
122+
// close
123+
curl_close($curl);
124+
125+
$json = json_decode($response, true);
126+
127+
if (
128+
!isset($json['meta']['status_code']) ||
129+
!isset($json['data'])
130+
) {
131+
throw new Exception('Invalid response');
132+
}
133+
134+
if ($json['meta']['status_code'] != 200) {
135+
throw new Exception(
136+
$json['meta']['status'],
137+
$json['meta']['status_code']
138+
);
139+
}
140+
141+
// we expect JSON, so decode it
142+
return $json['data'];
143+
}
144+
65145
/**
66146
* get the API key
67147
*

0 commit comments

Comments
 (0)