From 6239f9a6845421d0f797fd12fae6c8cb75638805 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franc=CC=A7ois=20Hodierne?= Date: Thu, 27 Nov 2014 12:15:50 +0100 Subject: [PATCH] support for json requests --- lib/Eyeem.php | 25 ++++++++++++++++++++++--- lib/Eyeem/Http.php | 6 +----- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/lib/Eyeem.php b/lib/Eyeem.php index 7c0072c..3a187c2 100644 --- a/lib/Eyeem.php +++ b/lib/Eyeem.php @@ -41,7 +41,7 @@ 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), @@ -49,7 +49,7 @@ public function request($endpoint, $method = 'GET', $params = array(), $header = 'params' => $params, 'clientId' => $this->getClientId(), 'accessToken' => $this->getAccessToken(), - 'header' => $header + 'headers' => $headers ); $response = Eyeem_Http::request($request); $array = json_decode($response['body'], true); @@ -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') @@ -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']; } diff --git a/lib/Eyeem/Http.php b/lib/Eyeem/Http.php index ddaad2f..87b41a3 100644 --- a/lib/Eyeem/Http.php +++ b/lib/Eyeem/Http.php @@ -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) {