From 1c503f13ff15f35e6eac3ded9e02a3dde4c3f359 Mon Sep 17 00:00:00 2001 From: Evan Seguin Date: Thu, 19 Jul 2012 10:50:31 -0700 Subject: [PATCH] Updated library to use cURL instead of file_get_contents --- main/mashape/http/AuthUtil.php | 2 +- main/mashape/http/HttpClient.php | 35 +++++++++++++++++++------------- main/mashape/http/UrlUtils.php | 4 ++-- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/main/mashape/http/AuthUtil.php b/main/mashape/http/AuthUtil.php index 58f79f4..6b31782 100755 --- a/main/mashape/http/AuthUtil.php +++ b/main/mashape/http/AuthUtil.php @@ -30,7 +30,7 @@ class AuthUtil { $header = ""; if (!($publicKey == null || $privateKey == null)) { $hash = hash_hmac("sha1", $publicKey, $privateKey); - $header = "X-Mashape-Authorization: " . base64_encode($publicKey . ":" . $hash) . "\r\n"; + $header = "X-Mashape-Authorization: " . base64_encode($publicKey . ":" . $hash); } return $header; } diff --git a/main/mashape/http/HttpClient.php b/main/mashape/http/HttpClient.php index e8dde94..c526ba1 100755 --- a/main/mashape/http/HttpClient.php +++ b/main/mashape/http/HttpClient.php @@ -63,24 +63,31 @@ class HttpClient { private static function execRequest($httpMethod, $url, $parameters, $publicKey, $privateKey) { $data = null; if ($httpMethod != HttpMethod::GET) { - $url = self::removeQueryString($url); + //$url = self::removeQueryString($url); $data = http_build_query($parameters); } - $headers = AuthUtil::generateAuthenticationHeader($publicKey, $privateKey); - $headers .= UrlUtils::generateClientHeaders(); - - $opts = array('http' => - array( - 'ignore_errors' => true, - 'method' => $httpMethod, - 'content' => $data, - 'header' => $headers - ) - ); + //$headers = array(); + $headers = array( + ); + $headers[] = AuthUtil::generateAuthenticationHeader($publicKey, $privateKey); + $headers[] = UrlUtils::generateClientHeaders(); - $context = stream_context_create($opts); - $response = @file_get_contents($url, false, $context); + $ch = curl_init (); + + // prepare the request + //curl_setopt($ch, CURLOPT_USERPWD, "username:password"); for basic auth + curl_setopt ($ch, CURLOPT_URL , $url); + if ($httpMethod != HttpMethod::GET) { + curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, $httpMethod); + //curl_setopt ($ch, CURLOPT_POST, 1); + curl_setopt ($ch, CURLOPT_POSTFIELDS, $data); + } + curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers); + $response = curl_exec($ch); + curl_close($ch); + return $response; } diff --git a/main/mashape/http/UrlUtils.php b/main/mashape/http/UrlUtils.php index 653aaa2..9449894 100755 --- a/main/mashape/http/UrlUtils.php +++ b/main/mashape/http/UrlUtils.php @@ -91,10 +91,10 @@ class UrlUtils { } public static function generateClientHeaders() { - $headers = "User-Agent: mashape-php/1.0: " . "\r\n"; + $headers = "User-Agent: mashape-php/1.0: "; return $headers; } } -?> \ No newline at end of file +?>