duan2428 2013-12-14 04:01
浏览 46
已采纳

使用simplexml和PHP阅读Steam Web API

So I've read a couple of topics on reading XML with simpleXML, but I'm having troubles with my specific case.

Currently I am trying to extract single values from this XML block that comes from a Steam web 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>

and I can output all of this to a web page with the following PHP code (code has come from another post, which was incredibly helpful

$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());

However when I attempt to change $xml->asXML() to another element like the documentation says to, like so:

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

I get an error saying that there is no data returned.

For the purpose of the example I am trying to retrieve the element <personastate> which indicates if the player is online or not.

  • 写回答

1条回答 默认 最新

  • dshakcsq64956 2013-12-14 21:10
    关注

    I'm pulling the solution you posted in the question out into this answer (and fixing a couple quoting errors).

    As you noted, don't use the root element in the SimpleXML syntax:

    INCORRECT echo '<p>'.$xml->response->players->player->personastate.'</p>';
    CORRECT   echo '<p>'.$xml->players->player->personastate.'</p>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀