nategood / httpful

A Chainable, REST Friendly, PHP HTTP Client. A sane alternative to cURL.
MIT License
1.74k stars 299 forks source link

UTF-8 response is not supported #246

Open Erfeniuses opened 8 years ago

Erfeniuses commented 8 years ago

Greetings.

I'm trying to use this great tool for getting JSON from http source, but the reponse "Content-Type: text/json; charset=utf-8" and Httpful just crashes by "Unable to parse response as JSON". The content is UTF-8 and cURL can do this (CURLOPT_ENCODING] = "UTF-8"). The site is http://data.foli.fi/siri/vm

bigbigant commented 7 years ago

I think it's not about UTF-8. The service responses with a header "encoding:gzip", so httpful have to unzip the contents.

alexjeen commented 7 years ago

Easy to fix:

$response = $this->prepare($api, 'get', $url)
    ->sendsJson()
    ->parseWith(function ($body) {
            $body = iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($body));
            return json_decode($body, false);
    })
    ->send()->body;