【发布时间】:2019-10-23 16:03:04
【问题描述】:
所以我认为我有一个数组,其中包含来自 API 的大量数据(虽然可能是 json,但我不确定:https://bitebtc.com/api/v1/market),我想提取一些特定数据,例如“百分比”。
我使用 json_decode() 方法尝试了 3 种相对相似的方法,但没有任何效果:/ 这是其中之一:
<?php
$string = "https://bitebtc.com/api/v1/market";
$decoded = file_get_contents($string);
$percent = $decoded->percent;
echo $percent;
As you can see in the link, the expected output would be something like 1.3 or at least a floating number between 0 and 10, but I got nothing, or a php notice: Trying to get property of non-object; since it is not an error I guess the problem doesn't come from the non-object property thing...
【问题讨论】:
-
如果
$decoded === null,您可能有解码问题。尝试var_dump或$decoded。 (您可能需要从该 url 获取,json_decode 需要一个有效的 JSON 字符串。) -
上面的代码将 $data 设置为一个字符串,即 url,它没有获取 json 结果。您正在尝试将 url 解码为 json 字符串。快速而肮脏的方法是使用 $json = file_get_contents($url);
-
@Progrock 确实 $decoded 是 NULL!这意味着我的 json_decode() 没有返回任何东西......嗯,我应该使用 fopen() 还是类似的东西呢?也许是 file_get_content ?
-
是的,试试
file_get_contents。 -
你没有对 uri 做任何事情——它实际上只是一个字符串,而不是一个数组。您需要先连接到该 uri..
标签: php arrays json decode file-get-contents