zxc
zxc

Reputation: 415

What is wrong with my PHP and json

I am trying to get a response from my json array:

stdClass Object ( [Foo] => stdClass Object ( [id] => 0001 [name] => Foo [profileIconId] => 550 [summonerLevel] => 30 [revisionDate] => 1408463933000 ) )

using my current code, I know that it is really easy to solve - but I don't know what I am doing wrong as I can't find anything similar to this from what I am searching:

api.php:

<?php
class runeapi {
    const get_id_na = 'https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/';
    const get_id_euw = 'https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/';
    const key = '...';

    public function getID($summoner_name) {
        $name = $summoner_name;
        $call = self::get_id_euw .$name;
        return $this->request($call);
    }
    private function request($call) {
        $url = $call. '?api_key=' .self::key;
        $json = file_get_contents($url);
        $decode = json_decode($json);
        $result = $decode; //<-- This is the Issue.

        return $result;
    }
}
?>

testing.php:

<?php
include('api.php');
$summoner_name = 'Foo';

$test = new runeapi;
$r = $test->getID($summoner_name);

print_r($r);
?>

$r returns $result

I'd like to be able to call for id but no matter where I tried looking, I couldn't find an example similar to what I have.

What I've tried:

Upvotes: 1

Views: 49

Answers (2)

zxc
zxc

Reputation: 415

I had to add another variable to request():

public function getID($summoner_name) {
    $name = $summoner_name;
    $call = self::get_id_euw .$name;
    return $this->request($call, $name); //<-- added $name
}
private function request($call, $name) { //<-- added $name
    $url = $call. '?api_key=' .self::key;
    $json = file_get_contents($url);
    $decode = json_decode($json);
    $result = $decode->{$name}->{'id'}; //<-- added $name

    return $result;
}

Upvotes: 0

Mohit S
Mohit S

Reputation: 14044

I believe this will work for you

private function request($call) {
        $url = $call. '?api_key=' .self::key;
        $json = file_get_contents($url);

        return $json;
    } 

No need to decode it.

Upvotes: 1

Related Questions