You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP获取Steam用户完整库存及物品数量问题求助

Fixing Steam Inventory API Issues in PHP: Full Inventory Fetch & Item Counting

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) into 2l=english—Steam can't parse this correctly.
  • Escaped &: Using HTML-escaped & instead of plain & for separating parameters can cause issues with file_get_contents parsing 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_items flag in the response and use the start parameter 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-while loop keeps fetching inventory batches until there are no more items left, using last_assetid to start each new page.
  • Accurate Item Counting: We group assets by classid to get the total quantity of each item type the user owns.
  • Cleaner Data Lookup: Storing descriptions with classid as 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:38:15