如何从枪口反应中解析stdClass对象

问题描述 投票:0回答:1

嗨,我使用Guzzle从laravel中获得来自外部Api的响应,而我的响应是这样的:

{ "status": { "timestamp": "2020-03-31T14:43:38.674Z", "error_code": 0, "error_message": null, "elapsed": 12, "credit_count": 1, "notice": null }, "data": { "BTC": { "id": 1, "name": "Bitcoin", "symbol": "BTC", "slug": "bitcoin", "num_market_pairs": 7673, "date_added": "2013-04-28T00:00:00.000Z", "tags": [ "mineable" ], "max_supply": 21000000, "circulating_supply": 18297137, "total_supply": 18297137, "platform": null, "cmc_rank": 1, "last_updated": "2020-03-31T14:42:42.000Z", "quote": { "USD": { "price": 6483.18973912, "volume_24h": 36336487377.4909, "percent_change_1h": 0.329128, "percent_change_24h": 2.42904, "percent_change_7d": -2.76632, "market_cap": 118623810853.6729, "last_updated": "2020-03-31T14:42:42.000Z" } } } } }

我的职能是:

$currencies = Currency::where('status',1)->get();
        $symbols = array();
        foreach ($currencies as $currency){
           array_push($symbols,$currency->sign);
        }
        $params = [
            'query' => [
                'symbol' => 'btc,ath'
            ]
        ];
        $client = new Client(['headers' => ['Accepts' => 'application/json','X-CMC_PRO_API_KEY'=>'*******-****-****-*****-********']]);
        $response = $client->request('GET', 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest',$params);
        $res = $response->getBody();
        echo $res;

我如何解析响应并在foreach中使用它来获取特定货币?

json laravel api guzzle
1个回答
0
投票

使用json_decode()函数

$res = $response->getBody();
$res = json_decode($res);
echo $res;
© www.soinside.com 2019 - 2024. All rights reserved.