0

I'm trying to decode json and display results with php but getting nothing back. Could anyone take a look and tell me what I'm doing wrong?

Here is an example of my php :

class League

{
public function __construct($region, $id, $summid)  {
    global $apiKey;
    global $errorAPI;
    $data = RIOT_API . "$region/v2.3/league/by-summoner/" . urlencode($summid) . "?api_key=$apiKey";
    $info = httpResp($data);
    if ($info[1] == false)
        {
        $League = json_decode(file_get_contents($data) , true);
        for ($i = 0; $i < count($League['entries']); $i++)
            {
            if ($League['entries'][$i]['playerOrTeamId'] == $summid)
                {
                $floor = $i;
                break;
                }
            }

        $this->leaguename = $League['entries'][$floor]['leagueName'];
        $this->tier = $League['entries'][$floor]['tier'];
        $this->points = $League['entries'][$floor]['leaguePoints'];
        $this->rank = $League['entries'][$floor]['rank'];
        $this->wins = $League['entries'][$floor]['wins'];
        $this->exists = true;
        }
      else
        {
        echo "Error " . $info[0] . " - " . array_search($info[0], $errorAPI);
        $this->exists = false;
        }
    }
}

Here is my php for outputting:

$newLeague1 = new League("euw", "Deew0n", "24361317");

echo '<br />League Name : ', $newLeague1->name;
echo "\n";
echo '<br />Position : ', $newLeague1->position;
echo "\n";
echo '<br />League Points : ', $newLeague1->leaguePoints;
echo "\n";
echo '<br />Wins/Losses: ', $newLeague1->wins, "/", $newLeague1->totalgames;
echo "\n";
echo '<br />Division Name: ', $newLeague1->tier, ' ', $newLeague1->rank;

And lastly this is the json I am dealing with : http://pastebin.com/9FKgrjeS

5
  • @Newbi3 Not in an echo statement they don't Commented Feb 24, 2014 at 22:54
  • Also do a var_dump($League); of $League and lets see what you get in that object. We'll know if it is properly parsed or not Commented Feb 24, 2014 at 22:54
  • What is httpResp()? Also, using globals is a terrible way to code. Instead, pass in the dependencies to your class where required. Commented Feb 24, 2014 at 22:56
  • I also suggest you enable proper error reporting as you're undoubtedly hitting some undefined index errors. Set display_errors = On and error_reporting = E_ALL in your php.ini file and restart your web server Commented Feb 24, 2014 at 23:00
  • In addition to @Phil's comments. Do your error logs say anything? I typically use $ tail -f /var/log/apache2/location_to_error.log as an example, in a terminal window and keep it running Commented Feb 24, 2014 at 23:04

1 Answer 1

1

Your JSON is:

[
    {
        "name": "Jayce's Emmissaries",
        "tier": "SILVER",
        "queue": "RANKED_SOLO_5x5",
        "entries": [ {...}, {...} ]
        "participantId": "24361317"
    }
]

...which is an array with 1 item: an object with 5 items.

If you can change the JSON, remove the outer [], otherwise change the code to access this [0]th entry.

Sign up to request clarification or add additional context in comments.

3 Comments

Nice catch! OP could also use $League = reset($League). I doubt they'd be able to change the response format as it appears to be from a remote API.
How would I change the code to access the entries? As Phil mentioned the response is from a remote API so unable to change the format
As Phil said: $League = reset($League); this sets $League to the first entry of the array, so e.g. $League['name'] should than be valid

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.