我的PHP加载有问题。我正在使用PHP调用《部落冲突》 API JSON。这是我要在JSON中找到的位置:“氏族”>“成员”>“ bestOpponentAttack”
{"clan": {
"destructionPercentage": {},
"tag": "string",
"name": "string",
"badgeUrls": {},
"clanLevel": 0,
"attacks": 0,
"stars": 0,
"expEarned": 0,
"members": [
{
"tag": "string",
"name": "string",
"mapPosition": 0,
"townhallLevel": 0,
"opponentAttacks": 0,
"bestOpponentAttack": {
"order": 0,
"attackerTag": "string",
"defenderTag": "string",
"stars": 0,
"destructionPercentage": 0
},
"attacks": [
{
"order": 0,
"attackerTag": "string",
"defenderTag": "string",
"stars": 0,
"destructionPercentage": 0
}
]
}
]
},
"teamSize": 0,
"opponent": {
"destructionPercentage": {},
"tag": "string",
"name": "string",
"badgeUrls": {},
"clanLevel": 0,
"attacks": 0,
"stars": 0,
"expEarned": 0,
"members": [
{
"tag": "string",
"name": "string",
"mapPosition": 0,
"townhallLevel": 0,
"opponentAttacks": 0,
"bestOpponentAttack": {
"order": 0,
"attackerTag": "string",
"defenderTag": "string",
"stars": 0,
"destructionPercentage": 0
},
"attacks": [
{
"order": 0,
"attackerTag": "string",
"defenderTag": "string",
"stars": 0,
"destructionPercentage": 0
}
]
}
]
},
"startTime": "string",
"state": "string",
"endTime": "string",
"preparationStartTime": "string"
}
这是我的PHP
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<?php
$clantag = "<CLAN OMITTED>";
$token =
"<TOKEN OMITTED>";
$url = "https://api.clashofclans.com/v1/clans/" . urlencode($clantag) . "/currentwar";
$ch = curl_init($url);
$headr = array();
$headr[] = "Accept: application/json";
$headr[] = "Authorization: Bearer ".$token;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headr);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($ch);
$data = json_decode($res, true);
curl_close($ch);
if (isset($data["reason"])) {
$errormsg = true;
}
$members = $data["clan"]["members"];
?>
<title><?php echo $data["clan"]["name"]; ?></title>
</head>
<body>
<?php
if (isset($errormsg)) {
echo "<p>", "Failed: ", $data["reason"], " : ", isset($data["message"]) ? $data["message"] : "", "</p></body></html>";
exit;
}
?>
<table border="1">
<tr>
<td>Map Position</td>
<td>Tag</td>
<td>Name</td>
<td>TH lvl</td>
<td>Opponent Attacks</td>
<td>Best Opponent Attack</td>
</tr>
<?php
foreach ($members as $member) {
?>
<tr>
<td><?php echo $member["mapPosition"]; ?></td>
<td><?php echo $member["tag"]; ?></td>
<td><?php echo $member["name"]; ?></td>
<td><?php echo $member["townhallLevel"]; ?></td>
<td><?php echo $member["opponentAttacks"]; ?></td>
<td><?php echo $member["bestOpponentAttack"]; ?></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
像“标签”,“名称”,“ mapPosition”,“ townhallLevel”,“ opponentAttacks”之类的东西都可以正常显示。当我添加“ bestOpponentAttack”时,出现此错误:
“注意:第68行的C:\ xampp \ htdocs \ index.php中的未定义索引:bestOpponentAttack”
我不知道自己在做什么错,已经为此工作了几天,也无法弄清楚为什么嵌套的JSON返回未定义状态。
好的,因此PHP中的所有数组都是可迭代的。你可以绝对地foreach bestOpponentAttack
:
php > var_dump($bestOpponentAttack);
array(5) {
["order"]=>
int(1)
["attackerTag"]=>
string(3) "abc"
["defenderTag"]=>
string(3) "def"
["stars"]=>
int(3)
["destructionPercentage"]=>
int(100)
}
php > foreach ($bestOpponentAttack as $key=>$value){echo "{$key} : {$value}\n";}
order : 1
attackerTag : abc
defenderTag : def
stars : 3
destructionPercentage : 100
我想第一个问题是你肯定知道bestOpponentAttack
是始终存在于JSON?
如果是这样,那么下一个问题就是$members[0]['bestOpponentAttack']
您要传递给的Array echo
,它仅将Strings作为参数。但它提出了一个更大的问题,即您想要的信息位于内部$members[0]['bestOpponentAttack']
,而不是内部$members[0]['bestOpponentAttack']
:
["members"]=>
array(1) {
[0]=>
array(7) {
["tag"]=>
string(6) "string"
["name"]=>
string(6) "string"
["mapPosition"]=>
int(0)
["townhallLevel"]=>
int(0)
["opponentAttacks"]=>
int(0)
["bestOpponentAttack"]=>
array(5) {
["order"]=>
int(0)
["attackerTag"]=>
string(6) "string"
["defenderTag"]=>
string(6) "string"
["stars"]=>
int(0)
["destructionPercentage"]=>
int(0)
}...
您已将整个Array委托给单个表单元格,但需要决定如何在单元格自身中呈现该Array信息的每一部分,然后适当地访问每一部分。您可以根据需要进行迭代,也可以直接访问该单元格中所需的数据(例如$members[0]['bestOpponentAttack']['order']
)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句