RESOStandards / RESO-WebAPI-Client-PHP

RESO Web API Open Source Reference Client written in PHP
Other
4 stars 2 forks source link

Accept-Encoding as Request Header #4

Open nader14y opened 5 years ago

nader14y commented 5 years ago

Accept-Encoding as Request Header

This library is sending only Accept and Authentication params as Header of the Request::request(). Some API providers like MLSGrid need to send more params (like Accept-Encoding) as a Header.

phoenixwade commented 5 years ago

I'd be very interested in this, MLSGrid is only communicating on a compressed stream starting on Aug 14th.

theodorebear commented 4 years ago

same! working with MLS Grid which requires compressed gzip.

theodorebear commented 4 years ago

can anyone verify that https://github.com/RESOStandards/RESO-WebAPI-Client-PHP/pull/5 works?

clauz commented 4 years ago

I forked this project for several reasons, this issue being one of them. I would be more than happy to push a PR.

carterterryw commented 3 years ago

can anyone verify that #5 works?

I can verify that the modifications do in fact work for MLSGrid requests. The only other modification I had to do was to gzdecode() the returned data since for some reason that piece was not automatic.

jaybeaton commented 4 months ago

The simplest solution is to update the curl options to set CURLOPT_ACCEPT_ENCODING. If this is set to an empty string, curl will automatically send the appropriate header based on the supported algorithms and automatically decode the response.

Sets the contents of the Accept-Encoding: header sent in an HTTP request, and enables decoding of a response when a Content-Encoding: header is received.

libcurl potentially supports several different compressed encodings depending on what support that has been built-in.

To aid applications not having to bother about what specific algorithms this particular libcurl build supports, libcurl allows a zero-length string to be set ("") to ask for an Accept-Encoding: header to be used that contains all built-in supported encodings.

This is the only change required for it to work correctly with MLSGrid (and any others, I'd guess), and it does not cause any problems with our other RESO providers:

diff --git a/lib/HttpClient/CurlClient.php b/lib/HttpClient/CurlClient.php
index f27f4c98f..53da6c1bf 100644
--- a/lib/HttpClient/CurlClient.php
+++ b/lib/HttpClient/CurlClient.php
@@ -173,6 +173,7 @@ public function request($method, $absUrl, $headers, $params, $hasFile)
         $opts[CURLOPT_CONNECTTIMEOUT] = $this->connectTimeout;
         $opts[CURLOPT_TIMEOUT] = $this->timeout;
         $opts[CURLOPT_HEADERFUNCTION] = $headerCallback;
+        $opts[CURLOPT_ACCEPT_ENCODING] = '';
         if($headers) {
             $opts[CURLOPT_HTTPHEADER] = $headers;
         }

From the curl docs:

This option was called CURLOPT_ENCODING before 7.21.6

The specific libcurl you are using must have been built with zlib to be able to decompress gzip and deflate responses, with the brotli library to decompress brotli responses and with the zstd library to decompress zstd responses.