Skip to content

Commit

Permalink
support for json requests
Browse files Browse the repository at this point in the history
  • Loading branch information
znarf committed Nov 27, 2014
1 parent db8b3b2 commit 6239f9a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
25 changes: 22 additions & 3 deletions lib/Eyeem.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ public function getApiUrl($endpoint)
return $url;
}

public function request($endpoint, $method = 'GET', $params = array(), $header = array())
public function request($endpoint, $method = 'GET', $params = array(), $headers = array())
{
$request = array(
'url' => $this->getApiUrl($endpoint),
'method' => $method,
'params' => $params,
'clientId' => $this->getClientId(),
'accessToken' => $this->getAccessToken(),
'header' => $header
'headers' => $headers
);
$response = Eyeem_Http::request($request);
$array = json_decode($response['body'], true);
Expand All @@ -59,6 +59,14 @@ public function request($endpoint, $method = 'GET', $params = array(), $header =
return $array;
}

public function jsonRequest($endpoint, $method = 'GET', $params = array(), $headers = array())
{
$headers[] = 'Content-Type: application/json';
$params = json_encode($params);
$response = $this->request($endpoint, $method, $params, $headers);
return $response;
}

public function getRessourceObject($type, $ressource = array())
{
// Support getUser('me')
Expand Down Expand Up @@ -182,9 +190,20 @@ public function getToken($code, $redirect_uri = null)

// Upload

public function getFile($filename)
{
// PHP 5.5 introduced a CurlFile object that deprecates the old @filename syntax
// See: https://wiki.php.net/rfc/curl-file-upload
if (function_exists('curl_file_create')) {
return curl_file_create($filename);
}
// Use the old style if using an older version of PHP
return "@{$filename}";
}

public function uploadPhoto($filename)
{
$params = array('photo' => "@$filename");
$params = array('photo' => $this->getFile($filename));
$response = $this->request('/photos/upload', 'POST', $params);
return $response['filename'];
}
Expand Down
6 changes: 1 addition & 5 deletions lib/Eyeem/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,12 @@ public static function request($options = array())
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
}
// Headers
$headers = array();
$headers = isset($headers) ? $headers : array();
if (isset($accessToken)) {
$headers[] = "Authorization: Bearer $accessToken";
} elseif (isset($clientId)) {
$headers[] = "X-Client-Id: $clientId";
}
// Extra Header
if ($header) {
$headers = array_merge($headers, $header);
}
// Parameters
if (!empty($params)) {
switch ($method) {
Expand Down

0 comments on commit 6239f9a

Please sign in to comment.