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 = "";
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;
}

View File

@@ -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();
$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);
$context = stream_context_create($opts);
$response = @file_get_contents($url, false, $context);
return $response;
}

View File

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