php - 这个货币转换器 api 是如何工作的,获取汇率?
问题描述
我正在尝试使用这个 api(货币兑换 api)。这将返回如下内容:
{
"from":"USD",
"to":"HKD",
"rate":7.7950999999999996958877090946771204471588134765625
}
我发送请求,file_get_contents()
这似乎有效。我的问题是我无法访问数组 key rate
。
$r = file_get_contents('https://cc.hirak.cc/usd_hkd');
print_r($r['rate']); // nothing is shown. This is the problem.
print_r($r); // result shows ( all the array )
我怎样才能只访问rate
密钥?
解决方案
你正在访问一个字符串而不是数组,试试这个代码,我认为这会起作用:
$r = json_encode(file_get_contents('https://cc.hirak.cc/usd_hkd'),true);
推荐阅读
- ecmascript-6 - 为什么 babel 会以这种方式转换默认参数?
- asp.net - 计算字段上的 AspNetCore NullReferenceException
- javascript - 带有当前网址的whatsapp聊天按钮
- html - UTF-8 的 HTML 验证错误
- javascript - javascript 列表到 HTML 列表
- scala - 如何将scala中的列表列扩展到多行
- spotfire - Tibco Spotfire Sum 每小时记录到每天,一天从 10:00 开始
- string - 使用 VBA 将可变文本字符串(时间戳)插入单元格
- javascript - 检查文本框javascript内的用户输入数组
- git - 分支名称中的空格导致 SVN 到 GIT 迁移问题