diff --git a/README b/README index 8aea173..3e63a66 100644 --- a/README +++ b/README @@ -1,4 +1,4 @@ -Mashape PHP client library v0.1 +Mashape PHP client library v0.2 Copyright (C) 2011 Mashape, Inc. For the documentation, please visit http://www.mashape.com/guide/consume/php diff --git a/mashapebase/exceptions/clientExceptionMessages.php b/mashapebase/exceptions/clientExceptionMessages.php index 8d3ae4d..479d354 100644 --- a/mashapebase/exceptions/clientExceptionMessages.php +++ b/mashapebase/exceptions/clientExceptionMessages.php @@ -30,6 +30,6 @@ define("EXCEPTION_NOTSUPPORTED_HTTPMETHOD", "HTTP method not supported. Only DEL define("EXCEPTION_SYSTEM_ERROR_CODE", 2000); define("EXCEPTION_EMPTY_REQUEST", "A request attempt was made to the component, but the response was empty. The component's URL may be wrong or the firewall may be blocking your outbound HTTP requests."); -define("EXCEPTION_JSONDECODE_REQUEST", "Can't deserialize the response JSON from the component. The json_decode function is missing on your server or the method returned an invalid JSON value."); +define("EXCEPTION_JSONDECODE_REQUEST", "Can't deserialize the response JSON from the component. The json_decode function is missing on your server or the method returned an invalid JSON value: %s"); ?> diff --git a/mashapebase/http/httpClient.php b/mashapebase/http/httpClient.php index 243d2ab..9f2787c 100644 --- a/mashapebase/http/httpClient.php +++ b/mashapebase/http/httpClient.php @@ -26,11 +26,7 @@ require_once(dirname(__FILE__) . "/../init/init.php"); require_once(dirname(__FILE__) . "/../exceptions/mashapeClientException.php"); - -define("METHOD", "_method"); -define("TOKEN", "_token"); -define("LANGUAGE", "_language"); -define("VERSION", "_version"); +require_once(dirname(__FILE__) . "/urlUtils.php"); class HttpMethod { @@ -42,7 +38,7 @@ class HttpMethod class HttpClient { - public static function call($baseUrl, $httpMethod, $method, $token, $parameters) { + public static function call($url, $httpMethod, $token, $parameters) { if (empty($parameters)) { $parameters = array(); } else { @@ -54,29 +50,30 @@ class HttpClient { unset($parameters[$key]); } else { // Convert every value to a string value - $parameters[$key] = (string)$parameters[$key]; + $parameters[$key] = (string)$parameters[$key]; } } } - $parameters[METHOD] = $method; $parameters[TOKEN] = $token; $parameters[LANGUAGE] = CLIENT_LIBRARY_LANGUAGE; $parameters[VERSION] = CLIENT_LIBRARY_VERSION; + $url = UrlUtils::addClientParameters($url); + $response; switch($httpMethod) { case HttpMethod::DELETE: - $response = self::doDelete($baseUrl, $parameters); + $response = self::doDelete($url, $parameters); break; case HttpMethod::GET: - $response = self::doGet($baseUrl, $parameters); + $response = self::doGet($url, $parameters); break; case HttpMethod::POST: - $response = self::doPost($baseUrl, $parameters); + $response = self::doPost($url, $parameters); break; case HttpMethod::PUT: - $response = self::doPut($baseUrl, $parameters); + $response = self::doPut($url, $parameters); break; default: throw new MashapeClientException(EXCEPTION_NOTSUPPORTED_HTTPMETHOD, EXCEPTION_NOTSUPPORTED_HTTPMETHOD_CODE); @@ -86,29 +83,30 @@ class HttpClient { } $responseObject = json_decode($response); if (empty($responseObject)) { - throw new MashapeClientException(EXCEPTION_JSONDECODE_REQUEST, EXCEPTION_SYSTEM_ERROR_CODE); + throw new MashapeClientException(sprintf(EXCEPTION_JSONDECODE_REQUEST, $response), EXCEPTION_SYSTEM_ERROR_CODE); } return $responseObject; } - public static function doGet($url, $parameters) { - $queryString = ""; + private static function replaceParameters($url, $parameters) { + $finalUrl = UrlUtils::getCleanUrl($url, $parameters); if (!empty($parameters)) { $keys = array_keys($parameters); for ($i = 0;$i @@ -138,7 +140,7 @@ class HttpClient { ); $context = stream_context_create($opts); - $response = @file_get_contents($url, false, $context); + $response = @file_get_contents($finalUrl, false, $context); return $response; } diff --git a/mashapebase/http/tokenUtil.php b/mashapebase/http/tokenUtil.php index 60c5525..953aad0 100644 --- a/mashapebase/http/tokenUtil.php +++ b/mashapebase/http/tokenUtil.php @@ -31,10 +31,10 @@ define("TOKEN_URL", "https://api.mashape.com/requestToken"); class TokenUtil { - public static function requestToken($apiKey) { - $parameters = array("apikey"=>$apiKey); + public static function requestToken($devKey) { + $parameters = array("devkey"=>$devKey); - $response = HttpClient::doGet(TOKEN_URL, $parameters); + $response = HttpClient::doPost(TOKEN_URL, $parameters); $jsonResponse = json_decode($response); if (empty($jsonResponse->errors)) { diff --git a/mashapebase/init/init.php b/mashapebase/init/init.php index 5d3dd13..cc1a5b4 100644 --- a/mashapebase/init/init.php +++ b/mashapebase/init/init.php @@ -27,5 +27,9 @@ require_once(dirname(__FILE__) . "/json.php"); define("CLIENT_LIBRARY_LANGUAGE", "PHP"); -define("CLIENT_LIBRARY_VERSION", "V01"); +define("CLIENT_LIBRARY_VERSION", "V02"); + +define("TOKEN", "_token"); +define("LANGUAGE", "_language"); +define("VERSION", "_version"); ?>