Skip to content

Commit 6239f9a

Browse files
committed
support for json requests
1 parent db8b3b2 commit 6239f9a

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

lib/Eyeem.php

+22-3
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ public function getApiUrl($endpoint)
4141
return $url;
4242
}
4343

44-
public function request($endpoint, $method = 'GET', $params = array(), $header = array())
44+
public function request($endpoint, $method = 'GET', $params = array(), $headers = array())
4545
{
4646
$request = array(
4747
'url' => $this->getApiUrl($endpoint),
4848
'method' => $method,
4949
'params' => $params,
5050
'clientId' => $this->getClientId(),
5151
'accessToken' => $this->getAccessToken(),
52-
'header' => $header
52+
'headers' => $headers
5353
);
5454
$response = Eyeem_Http::request($request);
5555
$array = json_decode($response['body'], true);
@@ -59,6 +59,14 @@ public function request($endpoint, $method = 'GET', $params = array(), $header =
5959
return $array;
6060
}
6161

62+
public function jsonRequest($endpoint, $method = 'GET', $params = array(), $headers = array())
63+
{
64+
$headers[] = 'Content-Type: application/json';
65+
$params = json_encode($params);
66+
$response = $this->request($endpoint, $method, $params, $headers);
67+
return $response;
68+
}
69+
6270
public function getRessourceObject($type, $ressource = array())
6371
{
6472
// Support getUser('me')
@@ -182,9 +190,20 @@ public function getToken($code, $redirect_uri = null)
182190

183191
// Upload
184192

193+
public function getFile($filename)
194+
{
195+
// PHP 5.5 introduced a CurlFile object that deprecates the old @filename syntax
196+
// See: https://wiki.php.net/rfc/curl-file-upload
197+
if (function_exists('curl_file_create')) {
198+
return curl_file_create($filename);
199+
}
200+
// Use the old style if using an older version of PHP
201+
return "@{$filename}";
202+
}
203+
185204
public function uploadPhoto($filename)
186205
{
187-
$params = array('photo' => "@$filename");
206+
$params = array('photo' => $this->getFile($filename));
188207
$response = $this->request('/photos/upload', 'POST', $params);
189208
return $response['filename'];
190209
}

lib/Eyeem/Http.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,12 @@ public static function request($options = array())
2626
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
2727
}
2828
// Headers
29-
$headers = array();
29+
$headers = isset($headers) ? $headers : array();
3030
if (isset($accessToken)) {
3131
$headers[] = "Authorization: Bearer $accessToken";
3232
} elseif (isset($clientId)) {
3333
$headers[] = "X-Client-Id: $clientId";
3434
}
35-
// Extra Header
36-
if ($header) {
37-
$headers = array_merge($headers, $header);
38-
}
3935
// Parameters
4036
if (!empty($params)) {
4137
switch ($method) {

0 commit comments

Comments
 (0)