PHP - 尝试使用file_get_contents

时间:2017-11-13 15:59:49

标签: php json decode currency

我试图从Open Exchange Rates.org获得外汇汇率。

虽然之前已经提出过这个问题,但所有答案对我都不起作用 - 我想知道它是否也是因为我使用的是PHP的file_get_contents函数。请有人告诉我这个例子有什么问题:

<?php
$json = file_get_contents("https://openexchangerates.org/api/latest.json?app_id=API_KEY_HERE&base=USD");

$exchangeRates = json_decode($json, true);
Echo $exchangeRates['GBP'];
?>
  • 请注意,我已从此代码中取出API密钥,以便其他人不会使用我的帐户。

当我运行此代码时,我什么都没得到。我真的不知道自己做错了什么。如果我跑:

print_r($exchangeRates)

我得到了整个JSON对象 - 所以它从服务器获取数据,但它并没有像我期望的那样将它转换为数组。

Array ( [disclaimer] => Usage subject to terms: https://openexchangerates.org/terms [license] => https://openexchangerates.org/license [timestamp] => 1510588800 [base] => USD [rates] => Array ( [AED] => 3.673158 [AFN] => 68.753903 [ALL] => 114.46 [AMD] => 487.235 [ANG] => 1.784109 [AOA] => 165.9225 [ARS] => 17.5388 [AUD] => 1.309283 [AWG] => 1.786752 [AZN] => 1.6985 [BAM] => 1.67695 [BBD] => 2 [BDT] => 83.286948 [BGN] => 1.676802 [BHD] => 0.378019 [BIF] => 1760 [BMD] => 1 [BND] => 1.36148 [BOB] => 6.906125 [BRL] => 3.287588 [BSD] => 1 [BTC] => 0.000149873308 [BTN] => 65.370647 [BWP] => 10.666861 [BYN] => 1.993904 [BZD] => 2.008904 [CAD] => 1.272095 [CDF] => 1581 [CHF] => 0.995447 [CLF] => 0.02345 [CLP] => 631.5 [CNH] => 6.655797 [CNY] => 6.64068 [COP] => 3014.54 [CRC] => 569.31 [CUC] => 1 [CUP] => 25.5 [CVE] => 94.925 [CZK] => 21.918042 [DJF] => 178.97 [DKK] => 6.37866 [DOP] => 47.8825 [DZD] => 115.025 [EGP] => 17.6509 [ERN] => 15.341062 [ETB] => 27.35 [EUR] => 0.857289 [FJD] => 2.060195 [FKP] => 0.76267 [GBP] => 0.76267 [GEL] => 2.567429 [GGP] => 0.76267 [GHS] => 4.455 [GIP] => 0.76267 [GMD] => 47.29 [GNF] => 9000 [GTQ] => 7.335989 [GYD] => 207.810851 [HKD] => 7.80141 [HNL] => 23.659203 [HRK] => 6.471 [HTG] => 63.686462 [HUF] => 267.317 [IDR] => 13534.413381 [ILS] => 3.54562 [IMP] => 0.76267 [INR] => 65.455 [IQD] => 1183 [IRR] => 34718.946723 [ISK] => 103.74 [JEP] => 0.76267 [JMD] => 126.672893 [JOD] => 0.709001 [JPY] => 113.5035 [KES] => 103.707447 [KGS] => 69.131479 [KHR] => 4037 [KMF] => 422.1 [KPW] => 900 [KRW] => 1121.01 [KWD] => 0.302615 [KYD] => 0.832817 [KZT] => 332.915 [LAK] => 8320 [LBP] => 1510.65 [LKR] => 153.548466 [LRD] => 123.089603 [LSL] => 14.493921 [LYD] => 1.375 [MAD] => 9.4825 [MDL] => 17.667938 [MGA] => 3182.5 [MKD] => 52.777664 [MMK] => 1354.4 [MNT] => 2450.589147 [MOP] => 8.028673 [MRO] => 355 [MUR] => 34.382 [MVR] => 15.390769 [MWK] => 725.395 [MXN] => 19.1687 [MYR] => 4.190589 [MZN] => 60.994761 [NAD] => 14.495 [NGN] => 357 [NIO] => 30.5975 [NOK] => 8.168229 [NPR] => 104.574331 [NZD] => 1.448918 [OMR] => 0.385017 [PAB] => 1 [PEN] => 3.242828 [PGK] => 3.203028 [PHP] => 51.21 [PKR] => 104.949 [PLN] => 3.6327 [PYG] => 5652.9 [QAR] => 3.834975 [RON] => 3.989481 [RSD] => 101.625374 [RUB] => 59.4557 [RWF] => 833.5 [SAR] => 3.749867 [SBD] => 7.792911 [SCR] => 13.405 [SDG] => 6.675 [SEK] => 8.39106 [SGD] => 1.361603 [SHP] => 0.76267 [SLL] => 7643.328121 [SOS] => 593.5 [SRD] => 7.448 [SSP] => 130.2634 [STD] => 20902.599895 [SVC] => 8.744932 [SYP] => 514.98999 [SZL] => 14.375 [THB] => 33.076 [TJS] => 8.805269 [TMT] => 3.5 [TND] => 2.507899 [TOP] => 2.254982 [TRY] => 3.876419 [TTD] => 6.758802 [TWD] => 30.1855 [TZS] => 2246.75 [UAH] => 26.4873 [UGX] => 3636 [USD] => 1 [UYU] => 29.252603 [UZS] => 8090 [VEF] => 10.04815 [VND] => 22717.086641 [VUV] => 106.193334 [WST] => 2.54784 [XAF] => 562.34503 [XAG] => 0.05866836 [XAU] => 0.00078199 [XCD] => 2.70255 [XDR] => 0.712471 [XOF] => 562.34503 [XPD] => 0.00100412 [XPF] => 102.301846 [XPT] => 0.00107018 [YER] => 250.275 [ZAR] => 14.512027 [ZMW] => 9.906977 [ZWL] => 322.355011 ) )

0 个答案:

没有答案