David
David

Reputation: 1997

Undefined property: GuzzleHttp\Psr7\Response::$Results - Laravel 5.2 using API

I'am trying to implement caching into my Laravel 5 API, but I'm having trouble. I'am getting this error right now:

ErrorException in MedalController.php line 19:
Undefined property: GuzzleHttp\Psr7\Response::$Results

Can anyone figure out why this is not getting my header? I have never really used caching before, so I'am probably missing something here

This is how I'am calling and getting my medal count for each player in a Halo 5 game:

GetDataController:

class GetDataController extends Controller {


    /**
     * Fetch a Players Arena Stats
     *
     * @param $gamertag
     * @return mixed
     */
    public function getPlayerArenaStats($gamertag) {

        $client = new GuzzleHttp\Client();

        $baseURL = 'https://www.haloapi.com/stats/h5/servicerecords/arena?players=' . $gamertag;

        $res = $client->request('GET', $baseURL, [
            'headers' => [
                'Ocp-Apim-Subscription-Key' => env('Ocp-Apim-Subscription-Key')
            ]
        ]);

        Cache::put('stats', $res, 10);

        if ($res->getStatusCode() == 200) {
            return $result = json_decode($res->getBody());
        } elseif ($res->getStatusCode() == 404) {
            return $result = redirect()->route('/');
        }

        return $res;
    }

}

My MedalControler which calls the header and tries to get all the medals for a player:

class MedalController extends Controller {

    /**
     * Get a Players Arena Medals
     *
     * @param $playerArenaMedalStats
     * @return mixed
     */
    public function getArenaMedals($playerArenaMedalStats) {


        $results = collect($playerArenaMedalStats->Results[0]->Result->ArenaStats->MedalAwards);

        $array = $results->sortByDesc('Count')->map(function ($item, $key) {
            return [
                'MedalId' => $item->MedalId,
                'Count' => $item->Count,
            ];
        });

        return $array;
    }


}

And this is how how get, decode and return medals to view:

class StatsController extends Controller {


    /**
     * Return all Stats for a particular player
     * 
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request) {

        if (Cache::has('stats')) {
            $playerArenaMedalStats = Cache::get('stats');
            $playerArenaMedalStatsArray = app('App\Http\Controllers\MedalController')->getArenaMedals($playerArenaMedalStats);
            $arenaMedals = json_decode($playerArenaMedalStatsArray, true);
        } else {
            app('App\Http\Controllers\GetData\GetDataController')->getPlayerArenaStats($gamertag);
        }

      // More stuff here, shortened for simplicicty

      return view('player.stats')->with('arenaMedals', $arenaMedals);

    }


}

Upvotes: 2

Views: 5407

Answers (2)

David
David

Reputation: 1997

As Ravisha Hesh said, I just had to change the cache in my getPlayerArenaStats to this:

 Cache::put('stats', json_decode($res->getBody()), 10);

I had to json_decode it first

Upvotes: 1

Ravisha Hesh
Ravisha Hesh

Reputation: 1504

It's return $res at the end of your getPlayerArenaStats method. It returns GuzzleHttp\Psr7\Response and you are trying to get $playerArenaMedalStats->Results[0] in your getArenaMedals method on MedalController

Upvotes: 0

Related Questions