Laravel pobieranie api za pomocą bundla guzzle

in #dlive6 years ago

Thumbnail

Witam dzisiaj pokaże jak łatwo można w laraverze za pomocą bundla guzzle pobrać api na swoją stronę www kod wygląda tak
view
@extends('adminlte::page')

@section('title', 'AdminLTE')

@section('content_header')

Dashboard


@stop

@section('content')

You are logged in!


{{$res->getStatusCode() }}
{{ $res->getBody() }}
@stop
getBody odpowiada za jsona pokaże wszystkie dane jakie posiada nasze api
controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use GuzzleHttp\Client;

class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}

/**
 * Show the application dashboard.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $client = new Client();
    $res = $client->request('GET', 'nasze api należy tutaj podać link i podać wszystko to co odpowiada za wyświetlenie naszego api to zależy po prostu od dokumentacji danego api', [
         'nazwa tokena z bazy danych' => ['wygenerowany token potrzebny do autentykacji danego użytkownika']
    ]);

    return view('home', compact('res') );
}

}
Mogę wystąpić problemy związane z klasą Client wystarczy po prostu wpisać naa górze use GuzzleHttp\Client; tą linijkę i powinno działać.
[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"clouds":{"all":56},"wind":{"speed":3.45,"deg":38.5004},"rain":{"3h":2.44},"sys":{"pod":"d"},"dt_txt":"2018-07-26 15:00:00"},{"dt":1532628000,"main":{"temp":297.992,"temp_min":297.992,"temp_max":297.992,"pressure":1014.26,"sea_level":1026.16,"grnd_level":1014.26,"humidity":68,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"clouds":{"all":36},"wind":{"speed":2.2,"deg":41.001},"rain":{},"sys":{"pod":"d"},"dt_txt":"2018-07-26 18:00:00"},{"dt":1532638800,"main":{"temp":295.761,"temp_min":295.761,"temp_max":295.761,"pressure":1015.1,"sea_level":1027.03,"grnd_level":1015.1,"humidity":77,"temp_kf":0},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"clouds":{"all":64},"wind":{"speed":1.96,"deg":28.5087},"rain":{},"sys":{"pod":"n"},"dt_txt":"2018-07-26 21:00:00"},{"dt":1532649600,"main":{"temp":294.755,"temp_min":294.755,"temp_max":294.755,"pressure":1015.22,"sea_level":1027.19,"grnd_level":1015.22,"humidity":84,"temp_kf":0},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"clouds":{"all":56},"wind":{"speed":1.35,"deg":341.505},"rain":{},"sys":{"pod":"n"},"dt_txt":"2018-07-27 00:00:00"},{"dt":1532660400,"main":{"temp":294.374,"temp_min":294.374,"temp_max":294.374,"pressure":1015.45,"sea_level":1027.46,"grnd_level":1015.45,"humidity":85,"temp_kf":0},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"clouds":{"all":76},"wind":{"speed":2.57,"deg":22.0046},"rain":{"3h":0.32},"sys":{"pod":"d"},"dt_txt":"2018-07-27 03:00:00"},{"dt":1532671200,"main":{"temp":295.469,"temp_min":295.469,"temp_max":295.469,"pressure":1015.94,"sea_level":1027.82,"grnd_level":1015.94,"humidity":87,"temp_kf":0},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"clouds":{"all":80},"wind":{"speed":2.62,"deg":60.0064},"rain":{"3h":2.35},"sys":{"pod":"d"},"dt_txt":"2018-07-27 06:00:00"},{"dt":1532682000,"main":{"temp":298.753,"temp_min":298.753,"temp_max":298.753,"pressure":1016.08,"sea_level":1027.9,"grnd_level":1016.08,"humidity":81,"temp_kf":0},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"clouds":{"all":68},"wind":{"speed":2.61,"deg":71.5051},"rain":{"3h":0.46},"sys":{"pod":"d"},"dt_txt":"2018-07-27 09:00:00"},{"dt":1532692800,"main":{"temp":300.464,"temp_min":300.464,"temp_max":300.464,"pressure":1015.62,"sea_level":1027.43,"grnd_level":1015.62,"humidity":71,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"clouds":{"all":48},"wind":{"speed":3.37,"deg":79.0002},"rain":{},"sys":{"pod":"d"},"dt_txt":"2018-07-27 12:00:00"},{"dt":1532703600,"main":{"temp":301.166,"temp_min":301.166,"temp_max":301.166,"pressure":1015.02,"sea_level":1026.97,"grnd_level":1015.02,"humidity":65,"temp_kf":0},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}],"clouds":{"all":64},"wind":{"speed":3.72,"deg":86.0024},"rain":{},"sys":{"pod":"d"},"dt_txt":"2018-07-27 15:00:00"},{"dt":1532714400,"main":{"temp":299.417,"temp_min":299.417,"temp_max":299.417,"pressure":1015.41,"sea_level":1027.38,"grnd_level":1015.41,"humidity":65,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"clouds":{"all":48},"wind":{"speed":3.23,"deg":78.501},"rain":{},"sys":{"pod":"d"},"dt_txt":"2018-07-27 18:00:00"},{"dt":1532725200,"main":{"temp":297.63,"temp_min":297.63,"temp_max":297.63,"pressure":1016.13,"sea_level":1028.01,"grnd_level":1016.13,"humidity":71,"temp_kf":0},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"clouds":{"all":68},"wind":{"speed":3.49,"deg":75.0001},"rain":{},"sys":{"pod":"n"},"dt_txt":"2018-07-27 21:00:00"},{"dt":1532736000,"main":{"temp":296.382,"temp_min":296.382,"temp_max":296.382,"pressure":1016.09,"sea_level":1028.02,"grnd_level":1016.09,"humidity":75,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03n"}],"clouds":{"all":36},"wind":{"speed":2.82,"deg":80.5024},"rain":{},"sys":{"pod":"n"},"dt_txt":"2018-07-28 00:00:00"},{"dt":1532746800,"main":{"temp":294.277,"temp_min":294.277,"temp_max":294.277,"pressure":1015.92,"sea_level":1027.87,"grnd_level":1015.92,"humidity":85,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"clouds":{"all":32},"wind":{"speed":1.42,"deg":62.0016},"rain":{},"sys":{"pod":"d"},"dt_txt":"2018-07-28 03:00:00"},{"dt":1532757600,"main":{"temp":297.962,"temp_min":297.962,"temp_max":297.962,"pressure":1016.08,"sea_level":1027.93,"grnd_level":1016.08,"humidity":76,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"clouds":{"all":44},"wind":{"speed":2.56,"deg":65.5032},"rain":{},"sys":{"pod":"d"},"dt_txt":"2018-07-28 06:00:00"},{"dt":1532768400,"main":{"temp":300.533,"temp_min":300.533,"temp_max":300.533,"pressure":1015.69,"sea_level":1027.6,"grnd_level":1015.69,"humidity":66,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"clouds":{"all":44},"wind":{"speed":3.93,"deg":72.502},"rain":{},"sys":{"pod":"d"},"dt_txt":"2018-07-28 09:00:00"},{"dt":1532779200,"main":{"temp":301.015,"temp_min":301.015,"temp_max":301.015,"pressure":1014.92,"sea_level":1026.86,"grnd_level":1014.92,"humidity":61,"temp_kf":0},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}],"clouds":{"all":76},"wind":{"speed":4.31,"deg":63.5},"rain":{},"sys":{"pod":"d"},"dt_txt":"2018-07-28 12:00:00"}],"city":{"id":6695624,"name":"Warszawa","coord":{"lat":52.2355,"lon":21.0419},"country":"PL"}}
Taki powinien być wynik na waszej stronie. Dla was najbardziej kluczowy powinny być 3 ostatnie linijki z nazwą miasta krajem i temperaturą.

My video is at DLive

Sort:  

Do you also used codeigniter before?

no i work in laravel, symfony and yii2

Ahh i see. Great!! :)

Proszę, nie spamuj w #pl-artykuly.

nie spamuję uczę ty się też może czegoś nauczysz.

Tag #pl-artykuly, jak sama nazwa mówi, jest przeznaczony do artykułów. Twój post średnio do tego pasuje.

Coin Marketplace

STEEM 0.30
TRX 0.12
JST 0.034
BTC 63900.40
ETH 3140.82
USDT 1.00
SBD 3.98