Steam API 与 PHP 来自 JSON

Steam API with PHP from JSON

我一点也不了解 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" link 打电话,但我不太确定该怎么做。我必须使用 CRUL。 当前 PHP 代码没有输出任何错误。也不输出任何东西

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

$result = json_decode($result);

然后像这样得到 link:

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

?>

你需要解码json并把他转化为数组

$structure = json_decode($result, true);

// Array of players.
$players = $structure['response']['players'];

foreach($players as $row) {
    echo $row['avatarfull'] . '<br>';
}

你需要做这样的事情

<?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);
}