【问题标题】:Steam API with PHP from JSON来自 JSON 的带有 PHP 的 Steam API
【发布时间】:2016-12-08 15:13:07
【问题描述】:

我完全不懂 PHP,我不确定如何调用 Steam API JSON 的特定元素

JSON 输出是...

{
"response": {
    "players": [
        {
            "steamid": "76561198059606697",
            "communityvisibilitystate": 3,
            "profilestate": 1,
            "personaname": "Nerve | RainDrops",
            "lastlogoff": 1481147845,
            "commentpermission": 1,
            "profileurl": "http://steamcommunity.com/id/finalfront/",
            "avatar": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/65/653e72d21d87b59b7ddb8c90422aace354124c1b.jpg",
            "avatarmedium": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/65/653e72d21d87b59b7ddb8c90422aace354124c1b_medium.jpg",
            "avatarfull": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/65/653e72d21d87b59b7ddb8c90422aace354124c1b_full.jpg",
            "personastate": 4,
            "primaryclanid": "103582791456413210",
            "timecreated": 1330551594,
            "personastateflags": 0,
            "loccountrycode": "US"
        }
    ]

}
}

PHP 是...

<?php
$steamkey = "";
$id_user = "76561198059606697";

$apifr = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=".$steamkey."&steamid=".$id_banuser."&avatar";


$ch = curl_init();

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_URL,$apifr);

$result=curl_exec($ch);

curl_close($ch);

?>

我想调用“Avatarfull”链接,但我不太确定该怎么做。我必须使用 CRUL。 当前的 PHP 代码没有输出任何错误。也不输出任何东西

【问题讨论】:

  • 你需要PHP中的链接或者你可以使用AJAX

标签: php json steam


【解决方案1】:

您可以使用json_decode 从中创建一个对象数组:

$result = json_decode($result);

然后得到这样的链接:

echo $result->response->players[0]->avatarfull

或者如果你有多个元素,你可以循环遍历:

$result = json_decode($result);
$result = $result->response->players;
foreach($result as $r){
    echo $r->avatarfull;
}

更新

请参阅this link 以查看它的实际效果。

第二个例子是this link

在您的代码中看起来像这样:

<?php
$steamkey = "";
$id_user = "76561198059606697";

$apifr = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=".$steamkey."&steamid=".$id_banuser."&avatar";


$ch = curl_init();

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_URL,$apifr);

$result=curl_exec($ch);

$result = json_decode($result);
$result = $result->response->players;

foreach($result as $r){
    echo $r->avatarfull;
}

curl_close($ch);

?>

【讨论】:

  • 评论不用于扩展讨论;这个对话是moved to chat
  • ^^ 对不起,由于代表没有讨论
  • 您好@RainDrops,您尝试进入聊天室了吗?
  • 是的,我现在可以进入了
【解决方案2】:

你需要做这样的事情

<?php

// this part obviously is do it by your curl call
$result = '{
"response": {
    "players": [
        {
            "steamid": "76561198059606697",
            "communityvisibilitystate": 3,
            "profilestate": 1,
            "personaname": "Nerve | RainDrops",
            "lastlogoff": 1481147845,
            "commentpermission": 1,
            "profileurl": "http://steamcommunity.com/id/finalfront/",
            "avatar": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/65/653e72d21d87b59b7ddb8c90422aace354124c1b.jpg",
            "avatarmedium": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/65/653e72d21d87b59b7ddb8c90422aace354124c1b_medium.jpg",
            "avatarfull": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/65/653e72d21d87b59b7ddb8c90422aace354124c1b_full.jpg",
            "personastate": 4,
            "primaryclanid": "103582791456413210",
            "timecreated": 1330551594,
            "personastateflags": 0,
            "loccountrycode": "US"
        }
    ]

}
}';

//lets work with this data

$res = json_decode($result);

if(isset($res->response->players[0])){
    print_r($res->response->players[0]->avatar);
}

【讨论】:

    【解决方案3】:

    你需要解码json并将他转换为数组。

    $structure = json_decode($result, true);
    
    // Array of players.
    $players = $structure['response']['players'];
    
    foreach($players as $row) {
        echo $row['avatarfull'] . '<br>';
    }
    

    【讨论】:

    • @Emiliano 是的。再次阅读 json_decode 文档以及第二个参数 true 的作用。
    • " 为 foreach() 提供的参数无效" "/avatar.php on line 25" 即 "foreach($players as $row) {"
    猜你喜欢
    • 1970-01-01
    • 2018-05-24
    • 1970-01-01
    • 1970-01-01
    • 2014-07-24
    • 2019-02-28
    • 1970-01-01
    • 1970-01-01
    • 2021-01-08
    相关资源
    最近更新 更多