2016-02-03 54 views
1

我正在製作一個與本地API進行通信的Laravel 5.2項目。我在處理Guzzle響應主體時遇到了問題。在laravel中處理guzzle 6 response body

我的控制器:

public function getClients(){ 
    $guzzle = new Client(); 
    try{ 

     $response = $guzzle->request('GET', 'http://localhost:3000/client')->getBody();   
     return view('home.clients', ['clients' => $response]); 

    }catch(ClientException $e){ 
    //Handling the exception 
    } 
} 

我的刀片查看:

<h2>Client list</h2> 

{{ $clients }}//Just to inspect it 

@forelse ($clients as $client) 
    <h3>{{ $client->name }}</h3> 
    <h3>{{ $client->email }}</h3> 
    <h3>{{ $client->country }}</h3> 
@empty 
    <h3>No clients here</h3> 
@endforelse 

環路或控制器沒有錯誤,也顯示在瀏覽器,但在循環的流對象不顯示任何東西

我已經閱讀了Guzzle 6的響應文檔,但對於像我這樣的新手來說,這並不明顯。

想法?

瀏覽器輸出: browser output

回答

2

您對此JSON與json_decode()解碼:

public function getClients(){ 
    $guzzle = new Client(); 
    try { 

     $response = json_decode($guzzle->request('GET', 'http://localhost:3000/client')->getBody());   
     return view('home.clients', ['clients' => $response]); 

    } catch(ClientException $e){ 
     //Handling the exception 
    } 
} 

而且,您可以從您的視圖中刪除{{ $clients }}//Just to inspect it

有關JSON和Guzzle的詳細信息,請參閱:Guzzle 6: no more json() method for responses

+0

謝謝。 'json_decode()'做了訣竅 – Andres