未定义的属性:GuzzleHttp \ Psr7 \ Response :: $ Results-使用API​​的Laravel 5.2 [英] Undefined property: GuzzleHttp\Psr7\Response::$Results - Laravel 5.2 using API

查看:227
本文介绍了未定义的属性:GuzzleHttp \ Psr7 \ Response :: $ Results-使用API​​的Laravel 5.2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的Laravel 5 API中实现缓存,但是遇到了麻烦.我现在收到此错误:

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

MedalController.php第19行中的

ErrorException:
未定义的属性:GuzzleHttp \ Psr7 \ Response :: $ Results

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

这是我如何致电并获得Halo 5游戏中每个玩家的奖牌计数:

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

GetDataController:

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;
    }

}

我的MedalControler调用标题,并尝试为玩家获取所有奖牌:

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);

    }


}

推荐答案

正如Ravisha Hesh所说,我只需要将getPlayerArenaStats中的缓存更改为此:

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

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

我必须先对其进行json_decode

I had to json_decode it first

这篇关于未定义的属性:GuzzleHttp \ Psr7 \ Response :: $ Results-使用API​​的Laravel 5.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆