2013-12-14 90 views
0

因此,我已閱讀了幾篇有關使用simpleXML讀取XML的主題,但我遇到了與特定案例有關的問題。使用simplexml和PHP讀取Steam Web API

目前,我試圖從該來自於蒸汽網絡API URL

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE response> 
<response> 
    <players> 
    <player> 
     <steamid>76561197996635228</steamid> 
     <communityvisibilitystate>3</communityvisibilitystate> 
     <profilestate>1</profilestate> 
     <personaname>P337</personaname> 
     <lastlogoff>1386990920</lastlogoff> 
     <commentpermission>1</commentpermission> 
     <profileurl>http://steamcommunity.com/id/P337/</profileurl> 
     <avatar>http://media.steampowered.com/steamcommunity/public/images/avatars/43/438f1b6217ee5600772cd1f9d978fbba999d8cc6.jpg</avatar> 
     <avatarmedium>http://media.steampowered.com/steamcommunity/public/images/avatars/43/438f1b6217ee5600772cd1f9d978fbba999d8cc6_medium.jpg</avatarmedium> 
     <avatarfull>http://media.steampowered.com/steamcommunity/public/images/avatars/43/438f1b6217ee5600772cd1f9d978fbba999d8cc6_full.jpg</avatarfull> 
     <personastate>0</personastate> 
     <primaryclanid>103582791431212131</primaryclanid> 
     <timecreated>1203124452</timecreated> 
     <personastateflags>0</personastateflags> 
    </player> 
    </players> 
</response> 

這個XML塊單值,我可以輸出所有的這一個網頁用下面的PHP代碼(代碼有來自另一篇文章,這是難以置信的幫助

$url = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=[STEAMAPIKEY]&steamids=[PROFILEID]&format=xml"; 
$get = file_get_contents($url); 
$xml = simplexml_load_string($get); 

header('Content-Type: text/xml'); 
die($xml->asXML()); 

然而,當我試圖改變$ XML-> asXML()像文檔其他元素來說,像這樣:

echo '<p>'.$xml->response->players->player->personastate.'</p>; 

我收到一條錯誤消息,說沒有返回數據。

出於示例的目的,我試圖檢索指示玩家是否在線的元素<personastate>

回答

1

我拉你在問題中發佈的解決方案到這個答案(和修復一對夫婦引用錯誤)。

正如你提到的,不要在SimpleXML語法使用的根元素:

INCORRECT echo '<p>'.$xml->response->players->player->personastate.'</p>'; 
CORRECT echo '<p>'.$xml->players->player->personastate.'</p>';