Mr Rost Mr Rost - 1 month ago 25
PHP Question

null when decoding JSON

I'm trying to decode JSON in my yii2 app, I get data from one API. I get that information and use var_dump to see it. It's look like that:

yii\ httpclient\ Response Object([client] => yii\ httpclient\ Client Object([baseUrl] => [formatters] => Array([urlencoded] => yii\ httpclient\ UrlEncodedFormatter Object([encodingType] => 1[charset] => ))[parsers] => Array()[requestConfig] => Array()[responseConfig] => Array()[contentLoggingMaxSize] => 2000[_transport: yii\ httpclient\ Client: private] => yii\ httpclient\ StreamTransport Object([_events: yii\ base\ Component: private] => Array()[_behaviors: yii\ base\ Component: private] => )[_events: yii\ base\ Component: private] => Array()[_behaviors: yii\ base\ Component: private] => Array())[_headers: yii\ httpclient\ Message: private] => Array([0] => HTTP / 1.1 200 OK[1] => Server: nginx[2] => Date: Mon, 31 Oct 2016 13: 49: 35 GMT[3] => Content - Type: application / json; charset = UTF - 8[4] => Connection: close[5] => Strict - Transport - Security: max - age = 31536000)[_cookies: yii\ httpclient\ Message: private] => [_content: yii\ httpclient\ Message: private] => [{
"ccy": "EUR",
"base_ccy": "UAH",
"buy": "28.10000",
"sale": "28.40000"
}, {
"ccy": "RUR",
"base_ccy": "UAH",
"buy": "0.40000",
"sale": "0.42000"
}, {
"ccy": "USD",
"base_ccy": "UAH",
"buy": "25.70000",
"sale": "25.90000"
}, {
"ccy": "BTC",
"base_ccy": "USD",
"buy": "665.7850",
"sale": "735.8676"
}][_data: yii\ httpclient\ Message: private] => [_format: yii\ httpclient\ Message: private] => [_events: yii\ base\ Component: private] => Array()[_behaviors: yii\ base\ Component: private] => )


In controller:

public function actionRates()
{
$model = new ExchangeRates();
$data = $model->getRate();
$goodData = json_decode($data, true);
return $this->render('about',['data' => $goodData]);
}


In view:

<?php var_dump($data) ?>


But when I'm trying do use json_decode and through var_dump see that I have NULL. I saw different solution for this issue but no one helps me:(

Fky Fky
Answer