koraktor / steam-condenser

A multi-language library for querying the Steam Community, Source, GoldSrc servers and Steam master servers
https://koraktor.de/steam-condenser
Other
356 stars 67 forks source link

WebApi json_decode problems #236

Closed majidarif closed 10 years ago

majidarif commented 10 years ago

Tried using

WebApi::setApiKey('secret');
$inventory = DotA2Inventory::createInventory('user');
print_r($inventory);

but was always getting "Trying to get property of non-object" from

protected function _getJSONData($interface, $method, $version = 1, $params = null) {
        $data = $this->getJSON($interface, $method, $version, $params);
        $result = json_decode($data)->result; /* this is where the error was */

        if($result->status != 1) {

Tried to catch the data returned from webapi request function and found a problem. Please check.

Used composer to install steam-condenser into my laravel 4 application.


Modified the _getJSONData function to catch the problem:

$data = $this->getJSON($interface, $method, $version, $params);
print_r($data);
die();
$result = json_decode($data)->result;

Notice that the json data will have a random character before the first { and another random character after the last }.

koraktor commented 10 years ago

Can you add more detail? For example what inventory are you trying to load and what "random characters" are added.

In its core, the WebApi class does nothing else than a really simple file_get_contents to an URL of Steam's Web API. So any additionally characters are probably produced by the Web API itself or by a proxy in between your machine and Steam.

majidarif commented 10 years ago

I am working with version 1.3.7 of steam-condenser. (checked my steam-condenser.php to confirm this) The error is "Trying to get property of non-object" on line 190 of WebApi.php

Specifically was trying to get the dota 2 inventory of the user 76561198115395760.

I tried this to check if the problem was from the API itself


$url = "http://api.steampowered.com/IEconItems_570/GetPlayerItems/v0001/?key=XXXX&steamid=76561198115395760";
$data = @file_get_contents($url);
print_r($data);

and it returned the correct data with no problem. But if I do it like this:

WebApi::setApiKey('XXXX');
$inventory = DotA2Inventory::createInventory('76561198115395760');
print_r($inventory);

"Trying to get property of non-object" error on line 190 of WebApi.php

$result = json_decode($data)->result;

As for the random characters added it kinda goes like this.

H{
"result": {
        "status": 1,
        "num_backpack_slots": 720,
        "items": [
            {
                "id": 1388056775,
                "original_id": 1388056775,
                "defindex": 15091,
                "level": 1,
                "quality": 4,
                "inventory": 1,
                "quantity": 1,
                "attributes": [
                    {
                        "defindex": 135,
                        "value": 0,
                        "float_value": 0
                    }
                ]

            },
        ]

    }
}s

the next time I refresh the page it could go like this (or any random characters at the beginning or the end)

Q{
"result": {
        "status": 1,
        "num_backpack_slots": 720,
        "items": [
            {
                "id": 1388056775,
                "original_id": 1388056775,
                "defindex": 15091,
                "level": 1,
                "quality": 4,
                "inventory": 1,
                "quantity": 1,
                "attributes": [
                    {
                        "defindex": 135,
                        "value": 0,
                        "float_value": 0
                    }
                ]

            },
        ]

    }
}b
koraktor commented 10 years ago

Strange, I'll try to reproduce this.

majidarif commented 10 years ago

Here is the screenshot of laravel 4 throwing the error.

steam-condenser-error

majidarif commented 10 years ago

Based on: http://wiki.teamfortress.com/wiki/Talk:WebAPI/GetPlayerItems

// Put together the URL for the backpack
$backpackURL = "http://api.steampowered.com/ITFItems_440/GetPlayerItems/v0001/?key=" . $APIkey . "&SteamID=" . $profile . "&format=json";

// Download and decode the json file
$userBackpack = json_decode(file_get_contents($backpackURL), true);

$result = $userBackpack['result'];
$items = $result['items'];

They seem to use the 2nd parameter of json_decode that makes the output be an array.

I think the real problem is the error "Trying to get property of non-object" on

$result = json_decode($data)->result;

or this

$url_contents = file_get_contents($backpack_url);
$backpack_json = json_decode($url_contents);

if($backpack_json->result->status != 1)
koraktor commented 10 years ago

The problem isn't caused by json_decode() returning a wrong data structure, it's caused by the broken initial JSON data.

I'm still unable to reproduce this problem with the given information. Can you send or gist (or whatever) a copy of your WebApi.php file. It's obviously not in the original state so there might be problem here.

majidarif commented 10 years ago

I'm currently using a fresh copy of steam-condenser. In original state, no modifications. I'll try to investigate more might be a compatibility issue.

from https://packagist.org/packages/koraktor/steam-condenser on my composer.json

"require": {
    "laravel/framework": "4.0.*",
    "hybridauth/hybridauth": "*",
    "koraktor/steam-condenser": "dev-master"
},

I'm using the Laravel 4 framework. Wamp Server: Apache 2.4.4, PHP 5.4.12. Running on windows 7.

Can I also see a sample of the actual output of

WebApi::setApiKey('XXXX');
$inventory = DotA2Inventory::createInventory('76561198115395760');
print_r($inventory);

Thank you for all the help so far, very much appreciated. :)

koraktor commented 10 years ago

See my answer to your question on SO. This may be caused by WAMP.

majidarif commented 10 years ago

I just moved everything to xampp. Done with the setup but with the worst luck, I can't test if its okay now. The GetPlayerItems api is returning 500 Internal Server Error on any machine I'm using.

koraktor commented 10 years ago

That's not your fault. Steam's Web API seems to have a general problem at the moment. Try again later.

koraktor commented 10 years ago

Just dumping the inventory will also output the item schema that's linked with it. The schema contains all item definitions.

majidarif commented 10 years ago

Everything is working well now. Thanks. I guess it was that bug with wamp. Ended up using xampp as you suggested.

Lastly

        echo "New<br />";

        foreach ($inventory->getPreliminaryItems() as $items){
            echo $items->getDefIndex() . ":" . $items->getName() . ":" . $items->getBackpackPosition();
            echo "<br />";
        }

        echo "<br />Inventory<br />";

        foreach ($inventory->getItems() as $items){
            echo $items->getDefIndex() . ":" . $items->getName() . ":" . $items->getBackpackPosition();
            echo "<br />";
        }

It takes too long to load. Like a minute or 53 sec to be exact. Am I doing this wrong?

koraktor commented 10 years ago

Can you be more specific about what takes long to load? Depending on your code there might be several requests to Web API and schema data might be several megabytes in size.

koraktor commented 10 years ago

I close this as the initial problem has been resolved.