使用 PHP 中的 API 响应数据。

huangapple go评论106阅读模式
英文:

using api response data in PHP

问题

Here is the translated code snippet:

  1. 我正在使用一个Laravel包,该包从欧洲银行获取最新的汇率。
  2. 当我调用

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

  1. 我得到

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

  1. 这一切都没问题。
  2. 然而,我想做的是获取这些汇率本身,并将它们呈现在一个简单的表格中。
  3. 我尝试了对 $rsp 进行 json_decode,但我得到了以下错误:
  4. > json_decode() 期望参数 1 是字符串,但给定了对象
英文:

I am using a Laravel package that gets the latest exchange rates from the European Bank.

When I call

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

I get

  1. BenMajor\ExchangeRatesAPI\Response {#571
  2. -response: GuzzleHttp\Psr7\Response {#566 ▶}
  3. -headers: array:15 [▶]
  4. -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 ▶"
  5. -body: {#554 ▶}
  6. -statusCode: 200
  7. -timestamp: "2020-01-03T09:22:49+00:00"
  8. -baseCurrency: "GBP"
  9. -rates: {#569
  10. +"CAD": 1.7151176498
  11. +"HKD": 10.279978309
  12. +"ISK": 161.385391616
  13. +"PHP": 66.9884943651
  14. +"DKK": 8.8082944311
  15. +"HUF": 388.9989154524
  16. +"CZK": 29.9559107842
  17. +"GBP": 1.0
  18. +"RON": 5.638232659
  19. +"SEK": 12.3459235158
  20. +"IDR": 18320.507379639
  21. +"INR": 94.1982600085
  22. +"BRL": 5.289527043
  23. +"RUB": 81.564224077
  24. +"HRK": 8.7759937756
  25. +"JPY": 143.5257226388
  26. +"THB": 39.7757815816
  27. +"CHF": 1.2808270854
  28. +"EUR": 1.1788560381
  29. +"MYR": 5.3949167728
  30. +"BGN": 2.3056066393
  31. +"TRY": 7.8628518885
  32. +"CNY": 9.1887112746
  33. +"NOK": 11.6008864997
  34. +"NZD": 1.9708115245
  35. +"ZAR": 18.5665110577
  36. +"USD": 1.3194935634
  37. +"MXN": 24.9190125902
  38. +"SGD": 1.7781864479
  39. +"AUD": 1.8868769746
  40. +"ILS": 4.5609940114
  41. +"KRW": 1530.4380629038
  42. +"PLN": 5.0153251285
  43. }
  44. }

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() expects parameter 1 to be string, object given

答案1

得分: 1

已经 $rsp 包含一个对象。因此,您不能再次解码它。
要获取汇率

  1. dd($rsp->rates);
英文:

Already $rsp contains an object. So you cant decode it again.
For getting the rates

  1. dd($rsp->rates);

答案2

得分: 1

  1. $lookup = new ExchangeRatesAPI();
  2. $rsp = $lookup->setBaseCurrency('GBP')->fetch();
  3. dd(json_decode($rsp->getRates()));

方法 getRates() 将返回汇率信息的数组 - 更多信息请参阅 ExchangeRatesAPI - 响应文档

英文:
  1. $lookup = new ExchangeRatesAPI();
  2. $rsp = $lookup->setBaseCurrency('GBP')->fetch();
  3. dd(json_decode($rsp->getRates());

method getRates() will return rates as array - more on ExchangeRatesAPI - response

答案3

得分: 0

你可以执行以下操作:

  1. $rates = $rsp->getRates();

然后通过循环遍历汇率。

BenMajor\ExchangeRatesAPI\Response 的源代码可在以下链接找到:https://github.com/benmajor/ExchangeRatesAPI/blob/master/src/Response.php

英文:

you can do:

  1. $rates = $rsp->getRates();

And a loop through the rates.

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

huangapple
  • 本文由 发表于 2020年1月3日 17:47:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576292.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定