David David - 5 months ago 58x
PHP Question

Get API data to display in view - Laravel 5.2

I have downloaded a small project on GitHub that uses the Halo 5 API. Right now if I type in a MY Gamer tag on the website, it will give me this response:


I want to display them individually in a view for example, not in json format.

This is the 3 methods I have right now:


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;


And this is my view:




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>


So How do I display for example this {"TotalShotsFired"}, into a view like: Total Shots Fired: {{ $player->TotalShotsFired }} ????


First of all, if you see your json data at the top you have shown doesnt have any parent Key. so you can append it by:

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

So you have something like:

{"player" :

Now you can access the array in view simply by typing :

{{ $player->TotalKills }}