多个XML查询问题

时间:2013-05-21 16:51:08

标签: xml xml-parsing

对于我正在处理的项目,我需要查询一个XML URL并使用预定的特许经营ID来检索玩家列表。使用这些播放器ID,我需要查询第二个XML URL并检索播放器名称和团队,然后将其显示在列表中。

这是我正在尝试做的事情:

首次质询

网址:http://football99.myfantasyleague.com/2007/export?TYPE=rosters&L=46184

  • 我需要选择在预定的特许经营权ID下列出的每个玩家ID。

第二次查询

网址:http://football.myfantasyleague.com/2013/export?TYPE=players&PLAYERS=3291

  • 在这里,我需要能够使用我们在第一个查询中从特定特许经营权ID中检索到的玩家ID来检索玩家姓名和团队。

在完成所有这些后,我希望将第二个查询的结果显示在列表中。

这意味着从第一个查询中可以检索到15个玩家ID,并且使用这些玩家ID我需要找到一种方法来获取每个玩家的名字和团队。

有人愿意帮助我吗?如果你问我,我可以提供更多信息。

// Parse and display
$data=array();

$url1 = "http://football99.myfantasyleague.com/2007/export?TYPE=rosters&L=46184";
$xml = simplexml_load_file($url1);
 foreach($xml->franchise->player as $franchise_one) {
  $id=(string) $franchise_one->attributes()->id;
  $data[$id]['id']=(string) $franchise_one->attributes()->id;
  $data[$id]['playerid']=(string) $franchise_one->attributes()->id;
 }

/* Query list to get player names and teams using the player ids from above */
$url2 = "http://football.myfantasyleague.com/2013/export?TYPE=players&PLAYERS=3291";
$xml_second = simplexml_load_file($url2);
 foreach($xml_second->players->player as $player) {
  $id=(string) $player->attributes()->id;
   $data[$id]['name']=(string) $player->attributes()->name;
 }

while(list($key,$franchise)=each($data)) {
 $list .= 'Franchise id: '.$franchise['id'].'<br/>Player id: '.$franchise['playerid'].'<br/>Player name: '.$player['name'].'<br/><br/>';
}

结果

Warning: Invalid argument supplied for foreach() in --- on line 41

Franchise id: 8025
Player id: 8025
 Player name: l

Franchise id: 7394
Player id: 7394
 Player name: l

Franchise id: 6780
Player id: 6780
 Player name: l

Franchise id: 7740
Player id: 7740
 Player name: l

Franchise id: 5004
Player id: 5004
 Player name: l

Franchise id: 5656
Player id: 5656
 Player name: l

Franchise id: 4914
Player id: 4914
 Player name: l

Franchise id: 6562
Player id: 6562
 Player name: l

Franchise id: 7403
Player id: 7403
 Player name: l

Franchise id: 7393
Player id: 7393
 Player name: l

Franchise id: 8266
Player id: 8266
 Player name: l

Franchise id: 6528
Player id: 6528
 Player name: l

Franchise id: 7653
Player id: 7653
 Player name: l

Franchise id: 6952
Player id: 6952
 Player name: l

Franchise id: 8339
Player id: 8339
 Player name: l

Franchise id: 8074
Player id: 8074
 Player name: l

Franchise id: 0521
Player id: 0521
 Player name: l

1 个答案:

答案 0 :(得分:0)

这就是诀窍。

$year         = '2007';
$league       = '46184';
$franchise_id = '0001';

// Parse and display
$data=array();

// Get players for each franchise
$url = 'http://football99.myfantasyleague.com/'.$year.'/export?TYPE=rosters&L='.$league.'&FRANCHISE='.$franchise_id.'';
$xml = simplexml_load_file($url);
 foreach($xml->franchise->player as $franchise_two) {
  $player_id=(string) $franchise_two->attributes()->id;
   $data[$player_id]['playerid']=(string) $franchise_two->attributes()->id;

    // Create a list of player ids
    $player_list = ''.$data[$player_id]['playerid'].',';

     // Parse players
     $players = simplexml_load_file('http://football.myfantasyleague.com/2013/export?TYPE=players&PLAYERS='.$player_list.'');
      foreach ($players as $player):
       $player_name=$player['name'];
      endforeach;

       // Display output
       while(list($key,$franchise)=each($data)) {
        $list .= 'Franchise id: '.$franchise_id.'<br/>Player id: '.$franchise['playerid'].'<br/>Player name: '.$player_name.'<br/><br/>';
       }
}

echo ''.$list.'';

回声结果

特许经营权ID:0001

玩家ID:8025

球员姓名:安德森,德里克

等等...