获取要在视图中显示的API数据 - Laravel 5.2

时间:2016-04-29 21:09:57

标签: php json api laravel

我在GitHub上下载了一个使用Halo 5 API的小项目。现在,如果我在网站上输入MY Gamer标签,它会给我这样的回复:

{"SpartanRank":114,"Xp":4291837,"TotalKills":12653,"TotalDeaths":10383,"Kd":1.218626601175,"GamesWon":480}

我想在视图中单独显示它们,例如,不是以json格式显示。

这是我现在拥有的3种方法:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface;


use App\User;
use GuzzleHttp;
use Illuminate\Support\Facades\Input;
use Illuminate\Validation\Validator;
use App\Http\Controllers\Controller;
use Symfony\Component\HttpFoundation\Response;

class ImageController extends Controller {


    public function generate(Request $request) {


        // Validate email and password.
        $this->validate($request, [
            'gamertag' => 'required|min:3',
        ]);

        $gamertag = Input::get('gamertag');

        // Get data from API
        $playerStats = $this->getPlayerStats($gamertag);
        $playerArray = $this->getPlayerArray($playerStats);              

         $spartanRank = json_decode($playerArray['SpartanRank'], true);
    $XP = json_decode($playerArray['Xp'], true);
    $TotalKills = json_decode($playerArray['TotalKills'], true);
    $TotalHeadshots = json_decode($playerArray['TotalHeadshots'], true);
    $TotalShotsFired = json_decode($playerArray['TotalShotsFired'], true);
    $TotalDeaths = json_decode($playerArray['TotalDeaths'], true);
    $Kd = json_decode($playerArray['Kd'], true);
    $GamesWon = json_decode($playerArray['GamesWon'], true);


    return view('stats')
        ->with('spartanRank', $spartanRank)
        ->with('XP', $XP)
        ->with('TotalKills', $TotalKills)
        ->with('TotalShotsFired', $TotalShotsFired)
        ->with('TotalHeadshots', $TotalHeadshots)
        ->with('TotalDeaths', $TotalDeaths)
        ->with('Kd', $Kd)
        ->with('GamesWon', $GamesWon);

    }


   protected function getPlayerStats($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' => 'MY SECRET KEY HERE'
            ]
        ]);

        if($res->getStatusCode() == 200)
        {
            return $result = json_decode($res->getBody());
        }

    }


   protected function getPlayerArray($playerStats) {
        $array = [];
        $array['SpartanRank'] = $playerStats->Results[0]->Result->SpartanRank;
        $array['Xp'] =          $playerStats->Results[0]->Result->Xp;
        $array['TotalKills'] =  $playerStats->Results[0]->Result->ArenaStats->TotalKills;
        $array['TotalDeaths'] = $playerStats->Results[0]->Result->ArenaStats->TotalDeaths;
        $array['Kd'] = $array['TotalKills'] / $array['TotalDeaths'];
        $array['GamesWon'] =    $playerStats->Results[0]->Result->ArenaStats->TotalGamesWon;

        return $array;
    }



}

这是我的观点:

@extends('home')

@section('content')

    @include('pages.partials.nav')

   Spartan Rank: {{ $spartanRank }} <br>
Spartan XP: {{ $XP }}<br>
Total Kills: {{ $TotalKills }}<br>
Total Headshots: {{ $TotalHeadshots }}<br>
Total Shots Fired: {{ $TotalShotsFired }}<br>
Total Deaths: {{ $TotalDeaths }}<br>
KD: {{ $Kd }}<br>
Games Won: {{ $GamesWon }}<br>

@stop

那么我如何将这个{&#34; TotalShotsFired&#34;}显示在如下视图中:Total Shots Fired:{{$ player-&gt; TotalShotsFired}} ????

1 个答案:

答案 0 :(得分:1)

首先,如果您在顶部看到您的json数据,则表明您没有任何父键。所以你可以通过以下方式附加它:

return response()->json(array('player' => $array));

所以你有类似的东西:

{"player" :
     {
     "SpartanRank":114,
     "Xp":4291837,
     "TotalKills":12653,
     "TotalDeaths":10383,
     "Kd":1.218626601175,
     "GamesWon":480
     } 
}

现在只需键入以下内容即可在视图中访问数组:

{{ $player->TotalKills }}