1 | <?php |
||
19 | class RESTfulService |
||
20 | { |
||
21 | protected $responseFormat = 'json'; |
||
22 | |||
23 | /** |
||
24 | * The API endpoint. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $endpoint; |
||
29 | |||
30 | /** |
||
31 | * The GuzzleHttp client to talk to the API. |
||
32 | * |
||
33 | * @var Client; |
||
34 | */ |
||
35 | protected $client; |
||
36 | |||
37 | /** |
||
38 | * The API key. |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $key; |
||
43 | |||
44 | /** |
||
45 | * The query parameter name for the key. |
||
46 | * For example, Last.fm use api_key, like this: |
||
47 | * https://ws.audioscrobbler.com/2.0?method=artist.getInfo&artist=Kamelot&api_key=API_KEY. |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $keyParam = 'key'; |
||
52 | |||
53 | /** |
||
54 | * The API secret. |
||
55 | * |
||
56 | * @var string |
||
57 | */ |
||
58 | protected $secret; |
||
59 | |||
60 | public function __construct($key, $secret, $endpoint, Client $client) |
||
67 | |||
68 | /** |
||
69 | * Make a request to the API. |
||
70 | * |
||
71 | * @param string $verb The HTTP verb |
||
72 | * @param string $uri The API URI (segment) |
||
73 | * @param bool $appendKey Whether to automatically append the API key into the URI. |
||
74 | * While it's usually the case, some services (like Last.fm) requires |
||
75 | * an "API signature" of the request. Appending an API key will break the request. |
||
76 | * @param array $params An array of parameters |
||
77 | * |
||
78 | * @return object|string |
||
79 | */ |
||
80 | public function request($verb, $uri, $appendKey = true, array $params = []) |
||
102 | |||
103 | /** |
||
104 | * Make an HTTP call to the external resource. |
||
105 | * |
||
106 | * @param string $method The HTTP method |
||
107 | * @param array $args An array of parameters |
||
108 | * |
||
109 | * @throws \InvalidArgumentException |
||
110 | * |
||
111 | * @return object |
||
112 | */ |
||
113 | public function __call($method, $args) |
||
125 | |||
126 | /** |
||
127 | * Turn a URI segment into a full API URL. |
||
128 | * |
||
129 | * @param string $uri |
||
130 | * @param bool $appendKey Whether to automatically append the API key into the URL. |
||
131 | * |
||
132 | * @return string |
||
133 | */ |
||
134 | public function buildUrl($uri, $appendKey = true) |
||
154 | |||
155 | /** |
||
156 | * @return Client |
||
157 | */ |
||
158 | public function getClient() |
||
162 | |||
163 | /** |
||
164 | * @param Client $client |
||
165 | */ |
||
166 | public function setClient($client) |
||
170 | |||
171 | /** |
||
172 | * @return string |
||
173 | */ |
||
174 | public function getKey() |
||
178 | |||
179 | /** |
||
180 | * @param string $key |
||
181 | */ |
||
182 | public function setKey($key) |
||
186 | |||
187 | /** |
||
188 | * @return string |
||
189 | */ |
||
190 | public function getSecret() |
||
194 | |||
195 | /** |
||
196 | * @param string $secret |
||
197 | */ |
||
198 | public function setSecret($secret) |
||
202 | |||
203 | /** |
||
204 | * @return string |
||
205 | */ |
||
206 | public function getEndpoint() |
||
210 | |||
211 | /** |
||
212 | * @param string $endpoint |
||
213 | */ |
||
214 | public function setEndpoint($endpoint) |
||
218 | } |
||
219 |