Updated library to use cURL instead of file_get_contents

This commit is contained in:
Evan Seguin
2012-07-19 10:50:31 -07:00
parent 04f9c98ad9
commit 1c503f13ff
3 changed files with 24 additions and 17 deletions

View File

@@ -30,7 +30,7 @@ class AuthUtil {
$header = ""; $header = "";
if (!($publicKey == null || $privateKey == null)) { if (!($publicKey == null || $privateKey == null)) {
$hash = hash_hmac("sha1", $publicKey, $privateKey); $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; return $header;
} }

View File

@@ -63,24 +63,31 @@ class HttpClient {
private static function execRequest($httpMethod, $url, $parameters, $publicKey, $privateKey) { private static function execRequest($httpMethod, $url, $parameters, $publicKey, $privateKey) {
$data = null; $data = null;
if ($httpMethod != HttpMethod::GET) { if ($httpMethod != HttpMethod::GET) {
$url = self::removeQueryString($url); //$url = self::removeQueryString($url);
$data = http_build_query($parameters); $data = http_build_query($parameters);
} }
$headers = AuthUtil::generateAuthenticationHeader($publicKey, $privateKey); //$headers = array();
$headers .= UrlUtils::generateClientHeaders(); $headers = array(
);
$headers[] = AuthUtil::generateAuthenticationHeader($publicKey, $privateKey);
$headers[] = UrlUtils::generateClientHeaders();
$opts = array('http' => $ch = curl_init ();
array(
'ignore_errors' => true, // prepare the request
'method' => $httpMethod, //curl_setopt($ch, CURLOPT_USERPWD, "username:password"); for basic auth
'content' => $data, curl_setopt ($ch, CURLOPT_URL , $url);
'header' => $headers 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);
$context = stream_context_create($opts);
$response = @file_get_contents($url, false, $context);
return $response; return $response;
} }

View File

@@ -91,7 +91,7 @@ class UrlUtils {
} }
public static function generateClientHeaders() { public static function generateClientHeaders() {
$headers = "User-Agent: mashape-php/1.0: " . "\r\n"; $headers = "User-Agent: mashape-php/1.0: ";
return $headers; return $headers;
} }