fixes
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
.DS_Store
|
||||
0
main/mashape/MashapeClient.php
Normal file → Executable file
0
main/mashape/MashapeClient.php
Normal file → Executable file
0
main/mashape/exceptions/ExceptionConstants.php
Normal file → Executable file
0
main/mashape/exceptions/ExceptionConstants.php
Normal file → Executable file
0
main/mashape/exceptions/MashapeClientException.php
Normal file → Executable file
0
main/mashape/exceptions/MashapeClientException.php
Normal file → Executable file
8
main/mashape/http/AuthUtil.php
Normal file → Executable file
8
main/mashape/http/AuthUtil.php
Normal file → Executable file
@@ -27,8 +27,12 @@
|
||||
class AuthUtil {
|
||||
|
||||
public static function generateAuthenticationHeader($publicKey, $privateKey) {
|
||||
$hash = hash_hmac("sha1", $publicKey, $privateKey);
|
||||
$header = "Proxy-Authorization: " . base64_encode($publicKey . ":" . $hash) . "\r\n";
|
||||
$header = "";
|
||||
if (!($publicKey == null || $privateKey == null)) {
|
||||
$uuid = uniqid();
|
||||
$hash = hash_hmac("sha1", $uuid, $privateKey);
|
||||
$header = "X-Mashape-Authorization: " . base64_encode($publicKey . ":" . $hash . $uuid) . "\r\n";
|
||||
}
|
||||
return $header;
|
||||
}
|
||||
|
||||
|
||||
46
main/mashape/http/Chunked.php
Executable file
46
main/mashape/http/Chunked.php
Executable file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
if (!function_exists('http-chunked-decode')) {
|
||||
/**
|
||||
* dechunk an http 'transfer-encoding: chunked' message
|
||||
*
|
||||
* @param string $chunk the encoded message
|
||||
* @return string the decoded message. If $chunk wasn't encoded properly it will be returned unmodified.
|
||||
*/
|
||||
function http_chunked_decode($chunk) {
|
||||
$pos = 0;
|
||||
$len = strlen($chunk);
|
||||
$dechunk = null;
|
||||
|
||||
while(($pos < $len)
|
||||
&& ($chunkLenHex = substr($chunk,$pos, ($newlineAt = strpos($chunk,"\n",$pos+1))-$pos)))
|
||||
{
|
||||
if (! is_hex($chunkLenHex)) {
|
||||
trigger_error('Value is not properly chunk encoded', E_USER_WARNING);
|
||||
return $chunk;
|
||||
}
|
||||
|
||||
$pos = $newlineAt + 1;
|
||||
$chunkLen = hexdec(rtrim($chunkLenHex,"\r\n"));
|
||||
$dechunk .= substr($chunk, $pos, $chunkLen);
|
||||
$pos = strpos($chunk, "\n", $pos + $chunkLen) + 1;
|
||||
}
|
||||
return $dechunk;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* determine if a string can represent a number in hexadecimal
|
||||
*
|
||||
* @param string $hex
|
||||
* @return boolean true if the string is a hex, otherwise false
|
||||
*/
|
||||
function is_hex($hex) {
|
||||
// regex is for weenies
|
||||
$hex = strtolower(trim(ltrim($hex,"0")));
|
||||
if (empty($hex)) { $hex = 0; };
|
||||
$dec = hexdec($hex);
|
||||
return ($hex == dechex($dec));
|
||||
}
|
||||
|
||||
?>
|
||||
18
main/mashape/http/HttpClient.php
Normal file → Executable file
18
main/mashape/http/HttpClient.php
Normal file → Executable file
@@ -25,6 +25,7 @@
|
||||
*/
|
||||
|
||||
require_once(dirname(__FILE__) . "/../json/Json.php");
|
||||
require_once(dirname(__FILE__) . "/Chunked.php");
|
||||
require_once(dirname(__FILE__) . "/../exceptions/MashapeClientException.php");
|
||||
require_once(dirname(__FILE__) . "/HttpMethod.php");
|
||||
require_once(dirname(__FILE__) . "/UrlUtils.php");
|
||||
@@ -32,7 +33,7 @@ require_once(dirname(__FILE__) . "/AuthUtil.php");
|
||||
|
||||
class HttpClient {
|
||||
|
||||
public static function doRequest($httpMethod, $url, $parameters, $mashapeAuthentication, $publicKey, $privateKey) {
|
||||
public static function doRequest($httpMethod, $url, $parameters, $publicKey, $privateKey, $encodeJson = true) {
|
||||
|
||||
if (!($httpMethod == HttpMethod::DELETE || $httpMethod == HttpMethod::GET ||
|
||||
$httpMethod == HttpMethod::POST || $httpMethod == HttpMethod::PUT)) {
|
||||
@@ -41,25 +42,32 @@ class HttpClient {
|
||||
|
||||
UrlUtils::prepareRequest($url, $parameters, ($httpMethod != HttpMethod::GET) ? true : false);
|
||||
|
||||
$response = self::execRequest($httpMethod, $url, $parameters, $mashapeAuthentication, $publicKey, $privateKey);
|
||||
$response = self::execRequest($httpMethod, $url, $parameters, $publicKey, $privateKey);
|
||||
|
||||
if (!$encodeJson) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$jsonResponse = json_decode($response);
|
||||
if (empty($jsonResponse)) {
|
||||
// It may be a chunked response
|
||||
$jsonResponse = json_decode(http_chunked_decode($response));
|
||||
if (empty($jsonResponse)) {
|
||||
throw new MashapeClientException(sprintf(EXCEPTION_JSONDECODE_REQUEST, $response), EXCEPTION_SYSTEM_ERROR_CODE);
|
||||
}
|
||||
}
|
||||
|
||||
return $jsonResponse;
|
||||
|
||||
}
|
||||
|
||||
private static function execRequest($httpMethod, $url, $parameters, $mashapeAuthentication, $publicKey, $privateKey) {
|
||||
private static function execRequest($httpMethod, $url, $parameters, $publicKey, $privateKey) {
|
||||
$data = null;
|
||||
if ($httpMethod != HttpMethod::GET) {
|
||||
$url = self::removeQueryString($url);
|
||||
$data = http_build_query($parameters);
|
||||
}
|
||||
|
||||
$headers = ($mashapeAuthentication) ? AuthUtil::generateAuthenticationHeader($publicKey, $privateKey) : "";
|
||||
$headers = AuthUtil::generateAuthenticationHeader($publicKey, $privateKey);
|
||||
$headers .= UrlUtils::generateClientHeaders();
|
||||
|
||||
$opts = array('http' =>
|
||||
|
||||
0
main/mashape/http/HttpMethod.php
Normal file → Executable file
0
main/mashape/http/HttpMethod.php
Normal file → Executable file
5
main/mashape/http/UrlUtils.php
Normal file → Executable file
5
main/mashape/http/UrlUtils.php
Normal file → Executable file
@@ -24,6 +24,9 @@
|
||||
*
|
||||
*/
|
||||
|
||||
define("CLIENT_LIBRARY_LANGUAGE", "PHP");
|
||||
define("CLIENT_LIBRARY_VERSION", "V04");
|
||||
|
||||
define("PLACEHOLDER_REGEX", "/\{([\w\.]+)\}/");
|
||||
class UrlUtils {
|
||||
|
||||
@@ -91,7 +94,7 @@ class UrlUtils {
|
||||
}
|
||||
|
||||
public static function generateClientHeaders() {
|
||||
$headers = "User-Agent: mashape-php/1.0: " . "\r\n";
|
||||
$headers = "X-Mashape-Language: " . CLIENT_LIBRARY_LANGUAGE . "\r\n" . "X-Mashape-Version: " . CLIENT_LIBRARY_VERSION . "\r\n";
|
||||
return $headers;
|
||||
}
|
||||
|
||||
|
||||
0
main/mashape/json/Json.php
Normal file → Executable file
0
main/mashape/json/Json.php
Normal file → Executable file
0
main/mashape/json/Services_JSON.php
Normal file → Executable file
0
main/mashape/json/Services_JSON.php
Normal file → Executable file
Reference in New Issue
Block a user