使用php和api显示蒸汽库存

时间:2014-04-20 18:57:20

标签: php json steam

当我查询http://api.steampowered.com/IEconItems_{appid}/GetPlayerItems/v0001/?key={apikey}&steamid={steamid}&format=json

时,我得到了json的回应

然而问题是我不知道如何处理这个响应而没有任何我无法找到的任何附加信息。在查看我自己的cs时获得的响应的一部分:去库存:

            {
            "id": 235322185,
            "original_id": 190991409,
            "defindex": 19,
            "level": 1,
            "quality": 4,
            "inventory": 70,
            "quantity": 1,
            "rarity": 4,
            "attributes": [
                {
                    "defindex": 6,
                    "value": 1130627072,
                    "float_value": 228
                },
                {
                    "defindex": 7,
                    "value": 1148436480,
                    "float_value": 975
                },
                {
                    "defindex": 8,
                    "value": 1031063904,
                    "float_value": 0.059762358665466309
                },
                {
                    "defindex": 147,
                    "value": "models\/weapons\/stattrack.mdl"
                },
                {
                    "defindex": 145,
                    "value": "models\/weapons\/w_smg_p90_mag.mdl"
                }
            ]

        },
        {
            "id": 236527226,
            "original_id": 236502674,
            "defindex": 27,
            "level": 1,
            "quality": 9,
            "inventory": 82,
            "quantity": 1,
            "rarity": 3,
            "attributes": [
                {
                    "defindex": 6,
                    "value": 1133608960,
                    "float_value": 291
                },
                {
                    "defindex": 7,
                    "value": 1142240880,
                    "float_value": 596.8505859375
                },
                {
                    "defindex": 8,
                    "value": 994750258,
                    "float_value": 0.0030927178449928761
                },
                {
                    "defindex": 80,
                    "value": 0,
                    "float_value": 0
                },
                {
                    "defindex": 81,
                    "value": 0,
                    "float_value": 0
                },
                {
                    "defindex": 147,
                    "value": "models\/weapons\/stattrack.mdl"
                },
                {
                    "defindex": 145,
                    "value": "models\/weapons\/w_shot_mag7_mag.mdl"
                }
            ]

        },
        {
            "id": 236529059,
            "original_id": 136971214,
            "defindex": 35,
            "level": 1,
            "quality": 4,
            "inventory": 77,
            "quantity": 1,
            "rarity": 2,
            "attributes": [
                {
                    "defindex": 6,
                    "value": 1077936128,
                    "float_value": 3
                },
                {
                    "defindex": 7,
                    "value": 1141712676,
                    "float_value": 564.611572265625
                },
                {
                    "defindex": 8,
                    "value": 1031160533,
                    "float_value": 0.060122329741716385
                },
                {
                    "defindex": 147,
                    "value": "models\/weapons\/stattrack.mdl"
                }
            ]

        }

id与武器相对应,稀有和数量也是有道理的。但我不能指望手动找到每个州的每个武器的每个id吗?我错过了一个不同的API,我可以用来查看什么武器与id是什么相关?

ps:抱歉英文不好

3 个答案:

答案 0 :(得分:3)

您将致电GetSchema以确定这些项目。您将比较DefIndex值与GetSchema

返回的值

您的工作流程如下所示:

  • 致电GetSchema并将result['items']的结果存储在某个地方以便稍后查看
  • 拨打当前电话并解析所有项目。对于每个项目,请从上面的
  • 中查找存储值中的结果
  • GetSchema还有您可能需要的稀有物品和其他值(即那些属性)

答案 1 :(得分:0)

尝试将JSON字符串解析为数组。因为此响应不是完全有效的JSON字符串,所以尝试将其插入到新数组中以解析有效的JSON字符串。

例如: {" main_container":[ - >您发布的代码< - ]}

<?php

    $jsonResponse = '{"main_container": [
                {
                "id": 235322185,
                "original_id": 190991409,
                "defindex": 19,
                "level": 1,
                "quality": 4,
                "inventory": 70,
                "quantity": 1,
                "rarity": 4,
                "attributes": [
                    {
                        "defindex": 6,
                        "value": 1130627072,
                        "float_value": 228
                    },
                    {
                        "defindex": 7,
                        "value": 1148436480,
                        "float_value": 975
                    },
                    {
                        "defindex": 8,
                        "value": 1031063904,
                        "float_value": 0.059762358665466309
                    },
                    {
                        "defindex": 147,
                        "value": "models\/weapons\/stattrack.mdl"
                    },
                    {
                        "defindex": 145,
                        "value": "models\/weapons\/w_smg_p90_mag.mdl"
                    }
                ]

            },
            {
                "id": 236527226,
                "original_id": 236502674,
                "defindex": 27,
                "level": 1,
                "quality": 9,
                "inventory": 82,
                "quantity": 1,
                "rarity": 3,
                "attributes": [
                    {
                        "defindex": 6,
                        "value": 1133608960,
                        "float_value": 291
                    },
                    {
                        "defindex": 7,
                        "value": 1142240880,
                        "float_value": 596.8505859375
                    },
                    {
                        "defindex": 8,
                        "value": 994750258,
                        "float_value": 0.0030927178449928761
                    },
                    {
                        "defindex": 80,
                        "value": 0,
                        "float_value": 0
                    },
                    {
                        "defindex": 81,
                        "value": 0,
                        "float_value": 0
                    },
                    {
                        "defindex": 147,
                        "value": "models\/weapons\/stattrack.mdl"
                    },
                    {
                        "defindex": 145,
                        "value": "models\/weapons\/w_shot_mag7_mag.mdl"
                    }
                ]

            },
            {
                "id": 236529059,
                "original_id": 136971214,
                "defindex": 35,
                "level": 1,
                "quality": 4,
                "inventory": 77,
                "quantity": 1,
                "rarity": 2,
                "attributes": [
                    {
                        "defindex": 6,
                        "value": 1077936128,
                        "float_value": 3
                    },
                    {
                        "defindex": 7,
                        "value": 1141712676,
                        "float_value": 564.611572265625
                    },
                    {
                        "defindex": 8,
                        "value": 1031160533,
                        "float_value": 0.060122329741716385
                    },
                    {
                        "defindex": 147,
                        "value": "models\/weapons\/stattrack.mdl"
                    }
                ]

            }
        ]}
    ';

    $jsonDecodeArray = json_decode($jsonResponse, true);

    foreach($jsonDecodeArray as $mainContainerItems){

        // LOOP THROUGH ALL ITEM GROUP. IN THIS CASE WE HAVE 3 ITEM GROUPS IN
        // $mainContainerItems ARRAY

        foreach($mainContainerItems as $inventoryItem){

            echo $inventoryItem['id']; // will return for example: 236529059
            echo $inventoryItem['original_id']; // will return for example: 136971214
            echo $inventoryItem['defindex'];
        }       
    }
?>

在这种情况下,resoult将是一个多维数组,您可以在其中循环使用多个foreach。

重生是:

$ jsonDecodeArray 包含所有项目的 main_container 数组,其中单个项目也是数组。

$ inventoryItem 包含一个项目,其中包含所有属性。如下所示。

Array
(
    [id] => 236529059
    [original_id] => 136971214
    [defindex] => 35
    [level] => 1
    [quality] => 4
    [inventory] => 77
    [quantity] => 1
    [rarity] => 2
    [attributes] => Array
        (
            [0] => Array
                (
                    [defindex] => 6
                    [value] => 1077936128
                    [float_value] => 3
                )

            [1] => Array
                (
                    [defindex] => 7
                    [value] => 1141712676
                    [float_value] => 564.61157226562
                )

            [2] => Array
                (
                    [defindex] => 8
                    [value] => 1031160533
                    [float_value] => 0.060122329741716
                )

            [3] => Array
                (
                    [defindex] => 147
                    [value] => models/weapons/stattrack.mdl
                )

        )

)

答案 2 :(得分:0)

将您的广告资源 defindex (非属性)值与此架构进行比较。

http://api.steampowered.com/IEconItems_730/GetSchema/v0002/?key=xxxxx

例如,defindex = 19是schema上的weapon_p90。