phpjsonlaravelguzzle

how to parse stdClass object from guzzle response


hi there i use Guzzle to get response from external Api in laravel and my response like this :

{ "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" } } } } }

and my function is :

$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;

how can i parse response and use it in foreach to get specific currency?


Solution

  • Use json_decode() function

    $res = $response->getBody();
    $res = json_decode($res);
    dd($res);