-2

I got this response by calling Steam Inventory API:

{  
    "result":{  
        "status":1,
        "items":[  
            {  
                "id":48331074,
                "original_id":48330920,
                "defindex":1013,
                "level":1,
                "quality":4,
                "inventory":1,
                "quantity":1,
                "rarity":6,
                "flag_cannot_trade":true,
                "flag_cannot_craft":true,
                "attributes":[  
                    {  
                        "defindex":69,
                        "value":1113325568,
                        "float_value":55
                    },
                    {  
                        "defindex":97,
                        "value":0,
                        "float_value":0
                    },
                    {  
                        "defindex":98,
                        "value":0,
                        "float_value":0
                    },
                    {  
                        "defindex":99,
                        "value":0,
                        "float_value":0
                    },
                    {  
                        "defindex":101,
                        "value":0,
                        "float_value":0
                    },
                    {  
                        "defindex":103,
                        "value":0,
                        "float_value":0
                    },
                    {  
                        "defindex":104,
                        "value":0,
                        "float_value":0
                    },
                    {  
                        "defindex":106,
                        "value":0,
                        "float_value":0
                    },
                    {  
                        "defindex":2,
                        "value":1,
                        "float_value":1.4012984643248171e-045
                    },
                    {  
                        "defindex":71,
                        "value":1,
                        "float_value":1.4012984643248171e-045
                    },
                    {  
                        "defindex":144,
                        "value":"models/inventory_items/bravo_bronze_01.mdl"
                    },
                    {  
                        "defindex":73,
                        "value":600,
                        "float_value":8.4077907859489024e-043
                    }
                ]
            },
            {  
                "id":210417056,
                "original_id":210416915,
                "defindex":1024,
                "level":1,
                "quality":4,
                "inventory":10,
                "quantity":1,
                "rarity":6,
                "flag_cannot_trade":true,
                "flag_cannot_craft":true,
                "attributes":[  
                    {  
                        "defindex":69,
                        "value":1137836032,
                        "float_value":420
                    },
                    {  
                        "defindex":97,
                        "value":4382,
                        "float_value":6.1404898706713484e-042
                    },
                    {  
                        "defindex":98,
                        "value":273,
                        "float_value":3.8255448076067506e-043
                    },
                    {  
                        "defindex":99,
                        "value":77,
                        "float_value":1.0789998175301091e-043
                    },
                    {  
                        "defindex":101,
                        "value":9,
                        "float_value":1.2611686178923354e-044
                    },
                    {  
                        "defindex":103,
                        "value":99,
                        "float_value":1.3872854796815689e-043
                    },
                    {  
                        "defindex":104,
                        "value":661,
                        "float_value":9.2625828491870408e-043
                    },
                    {  
                        "defindex":106,
                        "value":9,
                        "float_value":1.2611686178923354e-044
                    },
                    {  
                        "defindex":2,
                        "value":1,
                        "float_value":1.4012984643248171e-045
                    },
                    {  
                        "defindex":71,
                        "value":2,
                        "float_value":2.8025969286496341e-045
                    },
                    {  
                        "defindex":144,
                        "value":"models/inventory_items/phoenix_bronze_01.mdl"
                    },
                    {  
                        "defindex":73,
                        "value":600,
                        "float_value":8.4077907859489024e-043
                    }
                ]
            },
            {  
                "id":618061402,
                "original_id":467778052,
                "defindex":1030,
                "level":3,
                "quality":4,
                "inventory":38,
                "quantity":1,
                "rarity":6,
                "flag_cannot_trade":true,
                "flag_cannot_craft":true,
                "attributes":[  
                    {  
                        "defindex":97,
                        "value":2012,
                        "float_value":2.8194125102215319e-042
                    },
                    {  
                        "defindex":98,
                        "value":94,
                        "float_value":1.317220556465328e-043
                    },
                    {  
                        "defindex":99,
                        "value":22,
                        "float_value":3.0828566215145976e-044
                    },
                    {  
                        "defindex":101,
                        "value":2,
                        "float_value":2.8025969286496341e-045
                    },
                    {  
                        "defindex":103,
                        "value":54,
                        "float_value":7.5670117073540122e-044
                    },
                    {  
                        "defindex":104,
                        "value":282,
                        "float_value":3.9516616693959841e-043
                    },
                    {  
                        "defindex":171,
                        "value":24,
                        "float_value":3.363116314379561e-044
                    },
                    {  
                        "defindex":172,
                        "value":137,
                        "float_value":1.9197788961249994e-043
                    },
                    {  
                        "defindex":173,
                        "value":8,
                        "float_value":1.1210387714598537e-044
                    },
                    {  
                        "defindex":174,
                        "value":0,
                        "float_value":0
                    },
                    {  
                        "defindex":175,
                        "value":0,
                        "float_value":0
                    },
                    {  
                        "defindex":177,
                        "value":17,
                        "float_value":2.382207389352189e-044
                    },
                    {  
                        "defindex":179,
                        "value":4,
                        "float_value":5.6051938572992683e-045
                    },
                    {  
                        "defindex":180,
                        "value":1406160000,
                        "float_value":1789273309184
                    },
                    {  
                        "defindex":2,
                        "value":1,
                        "float_value":1.4012984643248171e-045
                    },
                    {  
                        "defindex":71,
                        "value":3,
                        "float_value":4.2038953929744512e-045
                    },
                    {  
                        "defindex":69,
                        "value":1065353216,
                        "float_value":1
                    },
                    {  
                        "defindex":144,
                        "value":"models/inventory_items/breakout_gold_01.mdl"
                    }
                ]
            },
            {  
                "id":1234103906,
                "original_id":901883020,
                "defindex":1318,
                "level":3,
                "quality":4,
                "inventory":19,
                "quantity":1,
                "rarity":6,
                "flag_cannot_trade":true,
                "flag_cannot_craft":true,
                "attributes":[  
                    {  
                        "defindex":97,
                        "value":4399,
                        "float_value":6.1643119445648703e-042
                    },
                    {  
                        "defindex":98,
                        "value":254,
                        "float_value":3.5592980993850354e-043
                    },
                    {  
                        "defindex":99,
                        "value":46,
                        "float_value":6.4459729358941585e-044
                    },
                    {  
                        "defindex":101,
                        "value":8,
                        "float_value":1.1210387714598537e-044
                    },
                    {  
                        "defindex":103,
                        "value":114,
                        "float_value":1.5974802493302915e-043
                    },
                    {  
                        "defindex":104,
                        "value":666,
                        "float_value":9.3326477724032817e-043
                    },
                    {  
                        "defindex":171,
                        "value":33,
                        "float_value":4.6242849322718963e-044
                    },
                    {  
                        "defindex":172,
                        "value":297,
                        "float_value":4.1618564390447067e-043
                    },
                    {  
                        "defindex":173,
                        "value":12,
                        "float_value":1.6815581571897805e-044
                    },
                    {  
                        "defindex":174,
                        "value":2,
                        "float_value":2.8025969286496341e-045
                    },
                    {  
                        "defindex":175,
                        "value":0,
                        "float_value":0
                    },
                    {  
                        "defindex":177,
                        "value":42,
                        "float_value":5.8854535501642317e-044
                    },
                    {  
                        "defindex":179,
                        "value":7,
                        "float_value":9.8090892502737195e-045
                    },
                    {  
                        "defindex":180,
                        "value":1415750400,
                        "float_value":3893589180416
                    },
                    {  
                        "defindex":2,
                        "value":1,
                        "float_value":1.4012984643248171e-045
                    },
                    {  
                        "defindex":71,
                        "value":4,
                        "float_value":5.6051938572992683e-045
                    },
                    {  
                        "defindex":69,
                        "value":1065353216,
                        "float_value":1
                    },
                    {  
                        "defindex":144,
                        "value":"models/inventory_items/vanguard_gold.mdl"
                    }
                ]
            },
            {  
                "id":2020603245,
                "original_id":2020603245,
                "defindex":4029,
                "level":1,
                "quality":4,
                "inventory":65,
                "quantity":1,
                "rarity":1,
                "attributes":[  
                    {  
                        "defindex":68,
                        "value":29,
                        "float_value":4.0637655465419695e-044
                    }
                ]
            },
            {  
                "id":2035927684,
                "original_id":1314827449,
                "defindex":4029,
                "level":1,
                "quality":4,
                "inventory":8,
                "quantity":1,
                "rarity":1,
                "attributes":[  
                    {  
                        "defindex":75,
                        "value":1428649200,
                        "float_value":11507542786048
                    },
                    {  
                        "defindex":68,
                        "value":29,
                        "float_value":4.0637655465419695e-044
                    }
                ]
            },
            {  
                "id":2035927705,
                "original_id":1294223748,
                "defindex":4029,
                "level":1,
                "quality":4,
                "inventory":51,
                "quantity":1,
                "rarity":1,
                "attributes":[  
                    {  
                        "defindex":75,
                        "value":1428649200,
                        "float_value":11507542786048
                    },
                    {  
                        "defindex":68,
                        "value":29,
                        "float_value":4.0637655465419695e-044
                    }
                ]
            },
            {  
                "id":2035927717,
                "original_id":1363427501,
                "defindex":4029,
                "level":1,
                "quality":4,
                "inventory":54,
                "quantity":1,
                "rarity":1,
                "attributes":[  
                    {  
                        "defindex":75,
                        "value":1428649200,
                        "float_value":11507542786048
                    },
                    {  
                        "defindex":68,
                        "value":29,
                        "float_value":4.0637655465419695e-044
                    }
                ]
            },
            {  
                "id":2035927729,
                "original_id":1363640267,
                "defindex":4029,
                "level":1,
                "quality":4,
                "inventory":53,
                "quantity":1,
                "rarity":1,
                "attributes":[  
                    {  
                        "defindex":75,
                        "value":1428649200,
                        "float_value":11507542786048
                    },
                    {  
                        "defindex":68,
                        "value":29,
                        "float_value":4.0637655465419695e-044
                    }
                ]
            },
            {  
                "id":2035927851,
                "original_id":1070414833,
                "defindex":4029,
                "level":1,
                "quality":4,
                "inventory":68,
                "quantity":1,
                "rarity":1,
                "attributes":[  
                    {  
                        "defindex":75,
                        "value":1428649200,
                        "float_value":11507542786048
                    },
                    {  
                        "defindex":68,
                        "value":29,
                        "float_value":4.0637655465419695e-044
                    }
                ]
            },
            {  
                "id":2035927879,
                "original_id":1161151661,
                "defindex":4029,
                "level":1,
                "quality":4,
                "inventory":66,
                "quantity":1,
                "rarity":1,
                "attributes":[  
                    {  
                        "defindex":75,
                        "value":1428649200,
                        "float_value":11507542786048
                    },
                    {  
                        "defindex":68,
                        "value":29,
                        "float_value":4.0637655465419695e-044
                    }
                ]
            },
            {  
                "id":2035927971,
                "original_id":1364196175,
                "defindex":4029,
                "level":1,
                "quality":4,
                "inventory":63,
                "quantity":1,
                "rarity":1,
                "attributes":[  
                    {  
                        "defindex":75,
                        "value":1428649200,
                        "float_value":11507542786048
                    },
                    {  
                        "defindex":68,
                        "value":29,
                        "float_value":4.0637655465419695e-044
                    }
                ]
            },
            {  
                "id":2035928178,
                "original_id":1351854817,
                "defindex":4029,
                "level":1,
                "quality":4,
                "inventory":55,
                "quantity":1,
                "rarity":1,
                "attributes":[  
                    {  
                        "defindex":75,
                        "value":1428649200,
                        "float_value":11507542786048
                    },
                    {  
                        "defindex":68,
                        "value":29,
                        "float_value":4.0637655465419695e-044
                    }
                ]
            },
            {  
                "id":2035928339,
                "original_id":1356562696,
                "defindex":4029,
                "level":1,
                "quality":4,
                "inventory":69,
                "quantity":1,
                "rarity":1,
                "attributes":[  
                    {  
                        "defindex":75,
                        "value":1428649200,
                        "float_value":11507542786048
                    },
                    {  
                        "defindex":68,
                        "value":29,
                        "float_value":4.0637655465419695e-044
                    }
                ]
            },
            {  
                "id":2036053269,
                "original_id":1266048733,
                "defindex":4029,
                "level":1,
                "quality":4,
                "inventory":70,
                "quantity":1,
                "rarity":1,
                "attributes":[  
                    {  
                        "defindex":75,
                        "value":1428649200,
                        "float_value":11507542786048
                    },
                    {  
                        "defindex":68,
                        "value":29,
                        "float_value":4.0637655465419695e-044
                    }
                ]
            },
            {  
                "id":2036053294,
                "original_id":1379110121,
                "defindex":4029,
                "level":1,
                "quality":4,
                "inventory":71,
                "quantity":1,
                "rarity":1,
                "attributes":[  
                    {  
                        "defindex":75,
                        "value":1428649200,
                        "float_value":11507542786048
                    },
                    {  
                        "defindex":68,
                        "value":29,
                        "float_value":4.0637655465419695e-044
                    }
                ]
            }
        ]
    }
}

I want to get the names of the items and count them, then print it like this:

Operation Phoenix Case | 21x
AWP Asiimov (Field-Tested) | 1x
Operation Vanguard Case | 65x
CS:GO Case Key | 2x

I want to store this data in a PHP array. I'm a beginner in that language, so can you explain me step by step what should I do? Thanks.

Kartm
  • 39
  • 1
  • 11
  • Can you explain to us step by step, what you have tried? As you have the data, can you show us what your desired output/result would look like? What names of the items are you wanting, and what are you counting? We don't know what your expected results are to be. – Sean May 10 '15 at 21:00
  • @Sean I have tried only calling for the inventory. I want my output to be something like this: Operation Phoenix Case | 21x AWP Asiimov (Field-Tested) | 1x Operation Vanguard Case | 65x CS:GO Case Key | 2x And I want to have this in PHP array. – Kartm May 10 '15 at 21:05
  • please edit your question with your desired result. It is difficult to see how you want it formatted in your comment. – Sean May 10 '15 at 21:12
  • I don't see `Operation Phoenix Case`, `AWP Asiimov (Field-Tested)`, `Operation Vanguard Case`, or `CS:GO Case Key` anywhere in your linked http://pastebin.com/8WupT8r0. Where are you getting those names/values from? – Sean May 10 '15 at 21:18
  • Those names are not in the data you received, so how are you supposed to parse them out of there? `json_decode` will turn your data into an array that you can traverse and count, but it is obviously not possible to get data out of it that isn't there in the first place... – Pevara May 10 '15 at 21:18
  • I've got for example this: _"id":48331074,_, I have no idea how to get the name. – Kartm May 10 '15 at 21:19
  • so how does `"id":48331074` relate to your desired results? Are we to guess that your `id` matches one of your names? Also, you give a count, but where does that count come from. Is that from your `"quantity":1` or from something else? – Sean May 10 '15 at 21:22
  • 1
    perhaps you should start at the beginning and show us what endpoint you are calling on the steam API – Pevara May 10 '15 at 21:24
  • I have found something. In the JSON I've called there is a "defindex" field. The same field is here: http://git.optf2.com/schema-tracking/plain/Counter%20Strike%20Global%20Offensive%20Schema?h=counterstrikeglobaloffensive . How to compare these two texts and get the names? – Kartm May 10 '15 at 21:30
  • compare what? you now have 2 json objects, but don't show how they join, and what information from those objects you want. give an example of what you want, if you want help. – Sean May 10 '15 at 21:35
  • First, I want to call for my inventory, it's already done. Then I want to compare every item in my inventory with this schema - http://git.optf2.com/schema-tracking/plain/Counter%20Strike%20Global%20Offensive%20Schema?h=counterstrikeglobaloffensive , get their names (and store them in an array), count the names that are repeating. – Kartm May 10 '15 at 21:45
  • 1
    that's great, good luck with that. since you can't clearly share with us how your 2 files are related, namely what names, ids, and quantities you are wanting, we can't help. Right now you just code dumped 2 huge json objects and asked us to help you get some data, but we don't know what data you want. Your result example does not match to either of your supplied code. That is like pointing to the ocean and asking us to count the sea life. way to broad, and unclear what you are asking. – Sean May 10 '15 at 21:50

1 Answers1

2

The best way to retreive user inventory in some game is to use that interface

http://steamcommunity.com/profiles/[steamid]/inventory/json/[appid]/2/

For example: [appid] is 730 for CS:GO

Here is the example you can retreive inventory of user in CS:GO

$steamid = 'PUT YOUR STEAMID';

$destUrl = 'http://steamcommunity.com/profiles/' . $steamid . '/inventory/json/730/2/';

$context = stream_context_create(array('http' => array('header'=>'Connection: close\r\n')));
$data = file_get_contents($destUrl, false, $context);
$data = json_decode($data, true);

As the answer you will get a json-object with a keys "rgInventory" and "rgDescriptions".

Good luck and do not forget to set your inventory to public in steam options.