Skip to content

Updated Request to add gzip compression for MLS Grid requirements #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion lib/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ abstract class Request
{
private static $validOutputFormats = array("json", "xml");
private static $requestAcceptType = "";
private static $requestAcceptEncoding = "";

/**
* Sends GET request and returns output in specified format.
Expand Down Expand Up @@ -53,8 +54,24 @@ public static function request($request, $output_format = "xml", $decode_json =
"Authorization: Bearer ".$token
);

/// enable GZIP compression for post MLSGRID Api
if(self::$requestAcceptEncoding) {
$headers[] = "Accept-Encoding:".self::$requestAcceptEncoding;
}

// Send request
$response = $curl->request("get", $url, $headers, null, false);

// Check if the response is gzip compressed
if( (isset($response[2]['Content-Encoding']) && $response[2]['Content-Encoding'] == 'gzip') || (isset($response[2]['content-encoding']) && $response[2]['content-encoding'] == 'gzip') ) {
// Decode the compressed data
$decodedData = gzdecode($response[0]);
if($decodedData === false) {
throw new Error\Api("Could not decode gzip compressed data.");
}
$response[0] = $decodedData;
}

if(!$response || !is_array($response) || $response[1] != 200) {
switch($response[1]) {
case "406":
Expand Down Expand Up @@ -113,6 +130,10 @@ public static function requestPost($request, $params = array())
"Authorization: Bearer ".$token
);

if(self::$requestAcceptEncoding) {
$headers[] = "Accept-Encoding:".$requestAcceptEncoding;
}

// Send request
$response = $curl->request("post", $url, $headers, $params, false);
if(!$response || !is_array($response) || $response[1] != 200) {
Expand Down Expand Up @@ -192,7 +213,7 @@ public static function setAcceptType($type = "") {
self::$requestAcceptType = $type;
}
}

/**
* Formats request parameters to compatible string
*
Expand All @@ -215,4 +236,14 @@ public static function formatRequestParameters($parameters_string) {

return implode("&", $params);
}

/**
* Sets accep encoding
*
* @param string
*/
public static function setAcceptEncoding($encoding = "") {
self::$requestAcceptEncoding = $encoding;
}

}