如何从此API的数组中获取特定值?

时间:2016-03-28 04:18:37

标签: javascript php jquery

http://roblox.plus:2052/inventory?username=joxxi

它显示了一个很大的长阵列。

第一部分是:

{"id":107484852,"username":"Joxxi","bc":"BC","rap":643

除了643

之外,我该如何忽略一切

我在这样的php脚本中使用api:

echo "<td><object height='20' width='100' data='http://roblox.plus:2052/inventory?username=".$row['username']."'/></td>";

现在它只是回显数组

3 个答案:

答案 0 :(得分:0)

尝试file_get_contents

$page = file_get_contents('http://roblox.plus:2052/inventory?username='.$row['username']);
echo json_decode($page,true)['rap'];

答案 1 :(得分:0)

简单做

$result = file_get_contents('http://roblox.plus:2052/inventory?username=joxxi');

然后将结果转换为关联数组

$row = json_decode($result, true);

稍后你可以回应但是在确保行不是数组的集合之前,如果是这样,则返回第一个

if (count ($row)) {
    $row = $row[0];
}
echo $row['rap']

希望有所帮助

答案 2 :(得分:0)

我还注意到比较curl和file_get_contents的执行时间有所不同。您还可以使用下面的卷曲代码来获取所需的数据。

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'http://roblox.plus:2052/inventory?username=joxxi');
$resultCurl = curl_exec($ch);
$obj = json_decode($resultCurl);
echo $obj->rap;