Updated library to use cURL instead of file_get_contents
This commit is contained in:
@@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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(
|
||||||
|
|
||||||
$opts = array('http' =>
|
|
||||||
array(
|
|
||||||
'ignore_errors' => true,
|
|
||||||
'method' => $httpMethod,
|
|
||||||
'content' => $data,
|
|
||||||
'header' => $headers
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
|
$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;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user