无法弄清楚为什么这个网络服务不起作用。只是给我空白。我测试了网址和数据。
我的代码:
session_start();
function getNews() {
$json = file_get_contents('http://onleague.stormrise.pt:8031/OnLeagueRest/resources/onleague/News/News?id_user=a7664093-502e-4d2b-bf30-25a2b26d6021&page=1&new_filter=0');
$data = json_decode($json, TRUE);
$newst = array();
foreach($data['data']['item'] as $item) {
$newst[] = $item;
}
foreach($newst as $v)
{
$_SESSION['newsid'][] = $v['id'];
$_SESSION['newstitle'][] = $v['title'];
$_SESSION['newstext'][] = $v['news'];
$_SESSION['newslink'][] = $v['link'];
$_SESSION['newsdate'][] = $v['date'];
$_SESSION['newsentityName'][] = $v['entityName'];
$_SESSION['aclikes'][] = $v['account']['likes'] . ")";
$_SESSION['acdislikes'][] = $v['account']['dislikes'] . ")";
$_SESSION['accomentes'][] = $v['account']['commentes'] . ")";
$_SESSION['acshares'][] = $v['account']['shares'] . ")";
$_SESSION['acclicks'][] = $v['account']['clicks'] . ")";
}
}
getNews();
$key = count($_SESSION['newsid']);
for ($i = 0; $i <= $key; $i++) {
echo $_SESSION['newsid'][$i] . "<br />";
}
答案 0 :(得分:1)
你确定这条线是正确的:
foreach($data['data']['item'] as $item) {
$newst[] = $item;
}
阅读本文让我觉得你用一个项目覆盖整个$ newstst数组......每个项目。因此,数组最终将成为 last $项的值(可能为空)。 我期待的是:
foreach($data['data']['item'] as $item) {
$newst.push( $item );
}
(注意,没有经过测试,我的语法可能很狡猾......但是你得到了漂移)。