public function makeRequest($method, $url, $params = null) { $curl = curl_init(); $method = strtolower($method); $headers = self::getHeaders(); $opts = array(); $credentials = Config::getCredentials(); if (empty($credentials) || empty($credentials['login']) || empty($credentials['apiToken'])) { throw new ApiException("Authorization error. Please ensure your login and API Token credentials are correct."); } $opts[CURLOPT_HTTPAUTH] = CURLAUTH_BASIC; $opts[CURLOPT_USERPWD] = $credentials['login'] . ':' . $credentials['apiToken']; $opts[CURLOPT_RETURNTRANSFER] = true; $opts[CURLOPT_CONNECTTIMEOUT] = 30; $opts[CURLOPT_TIMEOUT] = Config::DEFAULT_REQUEST_TIMEOUT; $opts[CURLOPT_HTTPHEADER] = $headers; switch ($method) { case 'get': if (count($params) > 0) { $url = "{$url}?" . http_build_query($params, null, '&'); } break; case 'post': $opts[CURLOPT_POST] = 1; $opts[CURLOPT_POSTFIELDS] = json_encode($params); break; case 'delete': $opts[CURLOPT_CUSTOMREQUEST] = 'DELETE'; if (count($params) > 0) { $url = "{$url}?" . http_build_query($params, null, '&'); } break; case 'put': $opts[CURLOPT_CUSTOMREQUEST] = 'PUT'; $opts[CURLOPT_POSTFIELDS] = json_encode($params); break; default: throw new ApiException("Unrecognized API Method: {$method}"); } $opts[CURLOPT_URL] = $url; curl_setopt_array($curl, $opts); $response = curl_exec($curl); if ($response === false) { $errno = curl_errno($curl); $message = curl_error($curl); curl_close($curl); throw new ApiException($message, $errno); } $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); try { $result = json_decode($response, true); } catch (Exception $e) { throw new ApiException("Invalid API Response: {$response}", $httpCode); } if ($httpCode < 200 || $httpCode >= 300) { throw new ApiException("API Error {$httpCode}: {$response}", $httpCode); } return $result; }
public function makeRequest($method, $url, $params = null) { $method = strtolower($method); $headers = self::getHeaders(); $credentials = Config::getCredentials(); if (empty($credentials) || empty($credentials['login']) || empty($credentials['apiToken'])) { throw new ApiException("Authorization error. Please ensure your login and API Token credentials are correct."); } $headers['Authorization'] = 'Basic ' . base64_encode($credentials['login'] . ':' . $credentials['apiToken']); $opts = array('timeout' => Config::DEFAULT_REQUEST_TIMEOUT, 'headers' => $headers); switch ($method) { case 'get': if (count($params) > 0) { $url = "{$url}?" . http_build_query($params, null, '&'); } $response = wp_remote_get($url, $opts); break; case 'post': $opts['body'] = json_encode($params); $response = wp_remote_post($url, $opts); break; case 'delete': $opts['method'] = 'DELETE'; if (count($params) > 0) { $url = "{$url}?" . http_build_query($params, null, '&'); } $response = wp_remote_request($url, $opts); break; case 'put': $opts['method'] = 'PUT'; $opts['body'] = json_encode($params); $response = wp_remote_request($url, $opts); break; default: throw new ApiException("Unrecognized API Method: {$method}"); } $response_code = wp_remote_retrieve_response_code($response); if ($response_code < 200 || $response_code >= 300) { $message = wp_remote_retrieve_response_message($response); if (empty($message)) { $message = "API Error ({$httpCode})"; } throw new ApiException($message, $response_code); } try { $result = json_decode(wp_remote_retrieve_body($response), true); } catch (Exception $e) { throw new ApiException("Invalid API Response: " . wp_remote_retrieve_body($response), $response_code); } return $result; }
protected function tearDown() { Config::setBrand(null); Config::setCredentials(null, null); }
public static function url() { return Config::getBaseUrl() . static::path(); }
public function testBaseUrlExceptionIfBrandNotSet() { Config::setBrand(null); $this->setExpectedException('Reamaze\\API\\Exceptions\\Config'); Config::getBaseUrl(); }