PHP获取Steam用户完整库存及物品数量问题求助
Hey there, let's break down the two key problems you're facing with the Steam Inventory API and walk through how to fix them properly.
Why You're Only Getting Partial Inventory Results
First, your API URL has critical formatting errors that are likely causing incomplete data:
Your current URL:
$inventoryJsonUrl = 'https://steamcommunity.com/inventory/76561198077278446/489940/2l=english&count=5000';
Key URL Mistakes:
- Missing
?for parameters: The Steam inventory endpoint follows this structure:https://steamcommunity.com/inventory/{steamId}/{appId}/{contextId}?{parameters}. You merged the context ID (2) with the first parameter (l=english) into2l=english—Steam can't parse this correctly. - Escaped
&: Using HTML-escaped&instead of plain&for separating parameters can cause issues withfile_get_contentsparsing the request. - No pagination handling: Even with a correct URL, if the user has more than 5000 items, Steam will only return the first batch. You need to check the
more_itemsflag in the response and use thestartparameter to fetch subsequent pages.
Why You Can't Count Item Quantities
Right now, you're looping through the descriptions array, which only lists unique item types—not how many of each the user actually owns. The real inventory instances live in the assets array: each entry here is a single item linked to a classid from descriptions. To count items, you need to group these assets by their classid.
Fixed Code Implementation
Here's the updated code that fixes both issues, handles pagination, and counts item quantities:
$steamId = '76561198077278446'; $appId = '489940'; $contextId = '2'; // Correct context ID for your game $language = 'english'; $batchSize = 5000; $startAssetId = 0; $allAssets = []; $itemDescriptions = []; // Fetch all inventory pages using pagination do { // Build the correctly formatted API URL $inventoryUrl = "https://steamcommunity.com/inventory/{$steamId}/{$appId}/{$contextId}?l={$language}&count={$batchSize}&start={$startAssetId}"; $inventoryResponse = file_get_contents($inventoryUrl); // Handle API request failures if (!$inventoryResponse) { die('Failed to retrieve inventory data from Steam'); } $inventoryData = json_decode($inventoryResponse, true); // Collect assets from this page if (!empty($inventoryData['assets'])) { $allAssets = array_merge($allAssets, $inventoryData['assets']); } // Store descriptions with classid as key for easy lookup if (!empty($inventoryData['descriptions'])) { foreach ($inventoryData['descriptions'] as $desc) { $itemDescriptions[$desc['classid']] = $desc; } } // Update pagination values for next batch $startAssetId = $inventoryData['last_assetid'] ?? 0; $hasMoreItems = $inventoryData['more_items'] ?? false; } while ($hasMoreItems); // Count items by their classid $itemCounts = []; foreach ($allAssets as $asset) { $classId = $asset['classid']; $itemCounts[$classId] = isset($itemCounts[$classId]) ? $itemCounts[$classId] + 1 : 1; } // Display items with their quantities foreach ($itemCounts as $classId => $count) { $item = $itemDescriptions[$classId]; $itemImage = "https://steamcommunity-a.akamaihd.net/economy/image/class/{$appId}/{$classId}/100fx60f"; echo '<div class="item"> <img src="' . $itemImage . '" alt="' . $item['market_name'] . '" /> <br />' . $item['market_name'] . ' (x' . $count . ') </div>'; }
Key Improvements Explained
- Fixed URL Structure: Corrected the parameter separation with
?and plain&so Steam can properly parse your request. - Pagination Handling: The
do-whileloop keeps fetching inventory batches until there are no more items left, usinglast_assetidto start each new page. - Accurate Item Counting: We group
assetsbyclassidto get the total quantity of each item type the user owns. - Cleaner Data Lookup: Storing descriptions with
classidas the key makes it easy to match counted assets to their item details. - Basic Error Handling: Added a check for failed API requests to avoid unexpected crashes.
内容的提问来源于stack exchange,提问作者Adam Fischer

