从此API输出PHP中的JSON数据

时间:2022-10-17 11:05:41

Wondering if anyone can help? I'm playing around with some API from a website and well.. it outputs the data a bit messy! From the data below I'm basically trying to abstract the 'balance' so I can then echo it via PHP..

想知道是否有人可以提供帮助?我正在玩一个网站上的一些API而且......它输出的数据有点乱!从下面的数据我基本上试图抽象'平衡',以便我可以通过PHP回应它..

Requests_Response Object ( 
[body] => {"status_code":200,"status_message":"OK","result":{"balance":0.50}} 
[raw] => HTTP/1.1 200 OK Server: nginx/1.10.2 Date: ### Content-Type: application/json Transfer-Encoding: chunked Connection: close Vary: Accept-Encoding X-Tracking-Token: ### Content-Encoding: gzip {"status_code":200,"status_message":"OK","result":{"balance":0.50}} 
[headers] => Requests_Response_Headers Object ( [data:protected] => Array ( [server] => Array ( [0] => nginx/1.10.2 ) 
[date] => Array ( [0] => ### ) 
[content-type] => Array ( [0] => application/json ) 
[vary] => Array ( [0] => Accept-Encoding ) 
[x-tracking-token] => Array ( [0] => ### ) 
[content-encoding] => Array ( [0] => gzip ) ) ) [status_code] => 200 [protocol_version] => 1.1 
[success] => 1 
[redirects] => 0 
[url] => ### 
[history] => Array ( ) 
[cookies] => Requests_Cookie_Jar Object ( [cookies:protected] => Array ( ) ) )

Thanks!

1 个解决方案

#1


1  

// $response is your Requests_Response Object

$body = json_decode($response->body, true);

if ($body['status_code'] == 200) {
    echo $body['result']['balance'];
}

#1


1  

// $response is your Requests_Response Object

$body = json_decode($response->body, true);

if ($body['status_code'] == 200) {
    echo $body['result']['balance'];
}