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 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧