简体   繁体   English

在 PHP 中使用 api 响应数据

[英]using api response data in PHP

I am using a Laravel package that gets the latest exchange rates from the European Bank.我正在使用从欧洲银行获取最新汇率的 Laravel 包。

When I call当我打电话

$lookup = new ExchangeRatesAPI();
$rsp  = $lookup->setBaseCurrency('GBP')->fetch();

I get我得到

BenMajor\ExchangeRatesAPI\Response {#571 ▼
  -response: GuzzleHttp\Psr7\Response {#566 ▶}
  -headers: array:15 [▶]
  -bodyRaw: "{"rates":{"CAD":1.7151176498,"HKD":10.279978309,"ISK":161.385391616,"PHP":66.9884943651,"DKK":8.8082944311,"HUF":388.9989154524,"CZK":29.9559107842,"GBP":1.0,"R ▶"
  -body: {#554 ▶}
  -statusCode: 200
  -timestamp: "2020-01-03T09:22:49+00:00"
  -baseCurrency: "GBP"
  -rates: {#569 ▼
    +"CAD": 1.7151176498
    +"HKD": 10.279978309
    +"ISK": 161.385391616
    +"PHP": 66.9884943651
    +"DKK": 8.8082944311
    +"HUF": 388.9989154524
    +"CZK": 29.9559107842
    +"GBP": 1.0
    +"RON": 5.638232659
    +"SEK": 12.3459235158
    +"IDR": 18320.507379639
    +"INR": 94.1982600085
    +"BRL": 5.289527043
    +"RUB": 81.564224077
    +"HRK": 8.7759937756
    +"JPY": 143.5257226388
    +"THB": 39.7757815816
    +"CHF": 1.2808270854
    +"EUR": 1.1788560381
    +"MYR": 5.3949167728
    +"BGN": 2.3056066393
    +"TRY": 7.8628518885
    +"CNY": 9.1887112746
    +"NOK": 11.6008864997
    +"NZD": 1.9708115245
    +"ZAR": 18.5665110577
    +"USD": 1.3194935634
    +"MXN": 24.9190125902
    +"SGD": 1.7781864479
    +"AUD": 1.8868769746
    +"ILS": 4.5609940114
    +"KRW": 1530.4380629038
    +"PLN": 5.0153251285
  }
}

All of which is fine.所有这些都很好。

However what I want to do is to get hold the rates themselves and present them in a simple table.然而,我想要做的是自己掌握利率并将它们呈现在一个简单的表格中。

I have tried to json_decode $rsp and I get我试图 json_decode $rsp 并且我得到

json_decode() expects parameter 1 to be string, object given json_decode() 期望参数 1 是字符串,给定的对象

Already $rsp contains an object. $rsp已经包含一个对象。 So you cant decode it again.所以你不能再次解码它。 For getting the rates为了获得费率

dd($rsp->rates);
$lookup = new ExchangeRatesAPI();
$rsp  = $lookup->setBaseCurrency('GBP')->fetch();
dd(json_decode($rsp->getRates());

method getRates() will return rates as array - more onExchangeRatesAPI - response方法 getRates() 将利率作为数组返回 -ExchangeRatesAPI 上的更多信息- 响应

you can do:你可以做:

$rates = $rsp->getRates();

And a loop through the rates.并通过费率循环。

Source of BenMajor\\ExchangeRatesAPI\\Response : https://github.com/benmajor/ExchangeRatesAPI/blob/master/src/Response.php BenMajor\\ExchangeRatesAPI\\Response 的来源: https : //github.com/benmajor/ExchangeRatesAPI/blob/master/src/Response.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM