-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.php
55 lines (47 loc) · 1.14 KB
/
client.php
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
<?php
/**
* Class companiesHouseApi
*/
final class companiesHouseApi {
const API_ENDPOINT = 'https://api.companieshouse.gov.uk';
private $api_key = null;
/**
* @param $api_key
*/
public function __construct($api_key) {
if (!empty($api_key)) {
$this->api_key = $api_key;
} else {
throw new InvalidArgumentException('Please supply a valid API key');
}
}
/**
* @param string $endpoint
* @param array $payload
*
* @return mixed
*/
public function send($endpoint, array $payload = []) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->getRequestUrl($endpoint, $payload));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $this->api_key . ':');
$result = curl_exec($ch);
curl_close($ch);
if ($json = json_decode($result, true)) {
$result = $json;
}
return $result;
}
/**
* @param string $endpoint
* @param array $payload
*
* @return string
*/
private function getRequestUrl($endpoint, array $payload) {
$payload = array_merge($payload, ['ts' => time()]);
$qs = '?' . http_build_query($payload);
return self::API_ENDPOINT . $endpoint . $qs;
}
}