Web服务数组问题

时间:2011-06-22 08:50:32

标签: php arrays web-services json

我有这个网络服务http://onleague.stormrise.pt:8031/OnLeagueRest/resources/onleague/Social/Login?Token=210029242357724|fd4eef8a839f24db2a9fedcd.1-100001001235070|Nro7dAY411DJRn7E8zB6MOXHjq8

我在捕获一些值时遇到问题:

clubId,clubName,clubLogo,relationType和dateAdded。 我只是不知道如何处理数组。 我的代码:

<?php
function getUserInfo() {
$json = file_get_contents('http://onleague.stormrise.pt:8031/OnLeagueRest/resources/onleague/Social/Login?Token=210029242357724|fd4eef8a839f24db2a9fedcd.1-100001001235070|Nro7dAY411DJRn7E8zB6MOXHjq8');
    $data = json_decode($json, TRUE);
    $v= $data['data'];
    $_SESSION['userinfid'][] = $v['id'];
    $_SESSION['userinfnickname'][] = $v['nickname'];
    $_SESSION['userinfvisibility'][] = $v['visibility'];
    $_SESSION['userinffirstname'][] = $v['first_name'];
    $_SESSION['userinflastname'][] = $v['last_name'];
    $_SESSION['userinfgender'][] = $v['gender'];
    $_SESSION['userinfdialect'][] = $v['dialect'];
    $_SESSION['userinfstatus'][] = $v['status'];
    $_SESSION['userinfadmissiondate'][] = $v['admission_date'];
    $_SESSION['userinflastaccess'][] = $v['last_access'];
    $_SESSION['userinfusername'][] = $v['username'];
    $_SESSION['userinfpoints'][] = $v['points'];
    $_SESSION['userinfranking'][] = $v['ranking'];
    $_SESSION['userinfsessionID'][] = $v['sessionID'];
    $_SESSION['userinfpublicProfile'][] = $v['publicProfile'];
    $_SESSION['userinfemail'][] = $v['email'];
    $_SESSION['userinfmobile'][] = $v['mobile'];
    $_SESSION['userinfimageURL'][] = $v['imageURL'];
    $_SESSION['userinfclubURL'][] = $v['clubURL'];
    $_SESSION['userinfcontact'][] = $v['contacts']['contact'];
    $_SESSION['userinfcontactType'][] = $v['contacts']['contactType'];
    $_SESSION['userinfisdefault'][] = $v['contacts']['is_default'];
    $_SESSION['userinfclubId'][] = $v['clubs']['clubId'];
    $_SESSION['userinfclubName'][] = $v['clubs']['clubName'];
    $_SESSION['userinfclubLogo'][] = $v['clubs']['clubLogo'];
    $_SESSION['userinfrelationType'][] = $v['clubs']['relationType'];
    $_SESSION['userinfdateAdded'][] = $v['clubs']['dateAdded'];
}
getUserInfo();

echo 'IDClube: ' . $_SESSION['userinfclubId'][0] . '<br />';
echo 'NomeClube: ' . $_SESSION['userinfclubName'][0] . '<br />'; 
echo 'LogoClube: ' . $_SESSION['userinfclubLogo'][0] . '<br />';
echo 'RelationType: ' . $_SESSION['userinfrelationType'][0] . '<br />';
echo 'DataAdicionado: ' . $_SESSION['userinfdateAdded'][0] . '<br />';

?>

2 个答案:

答案 0 :(得分:3)

如果每个会话密钥只有一个值,那么

<?php
function getUserInfo() {
   $json = file_get_contents('http://onleague.stormrise.pt:8031/OnLeagueRest/resources/onleague/Social/Login?Token=210029242357724|fd4eef8a839f24db2a9fedcd.1-100001001235070|Nro7dAY411DJRn7E8zB6MOXHjq8');
   $data = json_decode($json, TRUE);
   $v= $data['data'];
   foreach($v as $key => $value)
   { 
       $_SESSION['userinf'.$key] = $value;
   }
}



getUserInfo();
echo 'IDClube: ' . $_SESSION['userinfclubs']['clubId'] . '<br />';
echo 'NomeClube: ' . $_SESSION['userinfclubs']['clubName'] . '<br />'; 
echo 'LogoClube: ' . $_SESSION['userinfclubs']['clubLogo'] . '<br />';
echo 'RelationType: ' . $_SESSION['userinfclubs']['relationType'] . '<br />';
echo 'DataAdicionado: ' . $_SESSION['userinfclubs']['dateAdded'] . '<br />';

答案 1 :(得分:1)

小心,俱乐部里有多个俱乐部,所以你需要做这样的事情:

foreach ($v['clubs'] as $value) {
    $_SESSION['userinfclubId'][] = $value['clubId'];
    $_SESSION['userinfclubName'][] = $value['clubName'];
    $_SESSION['userinfclubLogo'][] = $value['clubLogo'];
    $_SESSION['userinfrelationType'][] = $value['relationType'];
    $_SESSION['userinfdateAdded'][] = $value['dateAdded'];
}