0

My code should convert "defindex" from array_inv into "item_name" from array_schema:

<?php
    $apikey = "X";
    $steamid = $steamprofile['steamid'];
    $url_inv = "http://api.steampowered.com/IEconItems_730/GetPlayerItems/v0001/?language=en?key=". $apikey . "&steamid=" . $steamid . "&format=json";
    $url_schema = "http://git.optf2.com/schema-tracking/plain/Counter%20Strike%20Global%20Offensive%20Schema?h=counterstrikeglobaloffensive";
    $array_inv_raw = file_get_contents($url_inv);
    $array_schema_raw = file_get_contents($url_schema);

    $array_inv = json_decode($array_inv_raw,true);
    $array_schema = json_decode($array_schema_raw,true);

    foreach($array_inv['result']['items'] as $item){
        foreach($array_schema['result']['items'] as $schemaItem){
            if($item['defindex'] == $schemaItem['defindex']){
                echo $schemaItem['item_name'].'<br />';
                break;
            }
        }
    }
?>

But it's resulting in these errors:

Warning: file_get_contents(http://api.steampowered.com/IEconItems_730/GetPlayerItems/v0001/?language=en?key=x&steamid=76561198037897388&format=json): failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in /home/kartm/public_html/scripts/inv.php on line 6

Warning: Invalid argument supplied for foreach() in /home/kartm/public_html/scripts/inv.php on line 12

The urls: array_inv array_schema

I can't find any mistakes. Can you tell me what's wrong with this code?

Kartm
  • 39
  • 1
  • 11
  • array_inv: http://tny.cz/55e2b83a and array_schema: http://tny.cz/7ab9eaaf – Kartm May 11 '15 at 20:45
  • read the error message - you're not allowed to fetch that url. since you're not fetching anything, you have no data to decode nor any array to loop on. Never ever assume an operation with an outside resource succeeded. Always check for failure and treat success as a pleasant surprise. – Marc B May 11 '15 at 20:46
  • The problem was _?language=en_, I just had to remove it. Solved. – Kartm May 11 '15 at 20:59

1 Answers1

0

These errors were caused by a typo in a url:

http://api.steampowered.com/IEconItems_730/GetPlayerItems/v0001/?language=en?key=x&steamid=76561198037897388&format=json

It should have been & key=x instead of ?key=x.

file_get_contents couldn't fetch the url, thus the foreach had an invalid, empty argument.

Kartm
  • 39
  • 1
  • 11