wrote this file. * As long as you retain this notice you can do whatever you want with * this stuff. If we meet some day, and you think this stuff is worth it, * you can buy me a beer in return. * * @author Sven Strittmatter * @copyright Copyright (c) 2010, Sven Strittmatter. * @version 0.2.0 * @license http://www.weltraumschaf.de/the-beer-ware-license.txt */ /** * @see Api_Log */ require_once 'Api/Log.php'; /** * */ class Api_Kwick_Request { const VERSION_20 = '2.0'; const DEFAULT_METHOD = 'getAPIVersion'; const DEFAULT_SERVICE = 'application'; private $baseUri; private $apiKey; private $secret; private $version; private $method; private $service; private $parameters; private $files; public function __construct(Api_Kwick_Config $config) { $this->setBaseUri($config->get('baseUri')); $this->setApiKey($config->get('apiKey')); $this->setSecret($config->get('secret')); $this->version = $config->get('version', self::VERSION_20); $this->service = self::DEFAULT_SERVICE; $this->method = self::DEFAULT_METHOD; $this->parameters = array(); $this->files = array(); } public function setBaseUri($uri) { $this->baseUri = (string) $uri; } public function getBaseUri() { return $this->baseUri; } public function setApiKey($key) { $this->apiKey = (string) $key; } public function getApiKey() { return $this->apiKey; } public function setFiles(array $files) { $this->files = $files; } public function setSecret($secret) { $this->secret = (string) $secret; } public function getSecret() { return $this->secret; } public function setParameter($name, $value) { Api_Log::log('Set parameter: ' . $name . ' with: ' . $value); $this->parameters[$name] = $value; } public function setMethod($method) { Api_Log::log('Set method to: ' . $method); $this->method = (string) $method; } public function setService($service) { Api_Log::log('Set service to: ' . $service); $this->service = (string) $service; } public function send(HttpRequest $httpRequest = null) { if (null === $httpRequest) { if (!class_exists('HttpRequest', false)) { throw new RuntimeException('Can not find class HttpRequest! Install pecl http.'); } $httpRequest = new HttpRequest(); } $url = $this->generateUri(); if (!empty($this->files)) { foreach ($this->files as $fileName => $fileUpload) { $httpRequest->addPostFile($fileName, $fileUpload['tmp_name']); } } Api_Log::log('Send request with url: ' . $url); $httpRequest->setUrl($url); $httpRequest->setMethod(HttpRequest::METH_POST); $start = microtime(true); $httpRequest->send(); $time = microtime(true) - $start; Api_Log::log('Duration of api request: ' . round($time, 3) . 's'); if ($httpRequest->getResponseCode() < 500) { switch ($httpRequest->getResponseCode()) { case 401: $message = 'Authroization required on URI: ' . $url; throw new RuntimeException($message, 401); case 404: $message = 'Not found URI: ' . $url; throw new RuntimeException($message, 404); default: return $httpRequest->getResponseBody(); } } $message = 'Got a server error response on URI: ' . $url; throw new RuntimeException($message, $httpRequest->getResponseCode()); } /** * @deprecated Please use generateUri() instead. * @return string */ public function generateUrl() { return $this->generateUri(); } /** * * @return string */ public function generateUri() { $url = $this->baseUri . '?'; $url .= 'api_key=' . $this->apiKey . '&'; $url .= 'v=' . $this->version . '&'; $url .= 'method=' . $this->getQualifiedMethod() . '&'; $url .= 'sig=' . $this->generateSignature(); if (!empty($this->parameters)) { foreach ($this->parameters as $name => $value) { $url .= '&' . $name . '=' . urlencode($value); } } return $url; } public function getQualifiedMethod() { return $this->service . '.' . $this->method; } public function generateParamString() { $parameters = array_merge($this->parameters, array( 'api_key' => $this->apiKey, 'v' => $this->version, 'method' => $this->getQualifiedMethod() )); ksort($parameters); $parameterString = ''; foreach ($parameters as $name => $value) { $parameterString .= $name . '=' . $value; } return $parameterString; } public function generateSignature() { $plain = $this->generateParamString() . $this->secret; Api_Log::log('Hashing plain signature: ' . $plain); return md5($plain); } }