2017-05-31 62 views
0

https://api.cinelist.co.uk/get/times/cinema/10565GOUTTE履帶式轉換JSON

此鏈接給我的標題和倍電影院正在播放特定電影的JSON列表。我想獲取這些信息並將其轉換爲字符串,但這怎麼可能?我已經使用json_decode,它說節點列表是空的。

這裏是我的代碼:

function odeon(){ 
    $client = new Client(); 
    $crawler = $client->request('GET', 'https://api.cinelist.co.uk/get/times/cinema/10565'); 
    //print $crawler->text()."\n"; 
    json_decode($crawler); 
    print_r($crawler); 
} 
+0

'的print_r(json_decode($履帶));' – Sandeesh

+0

它說,json_decode需要一個字符串,而是獲得是一個對象 – Przemek

+0

的DomCrawler對象似乎沒有任何方法來轉儲完整的響應數據。如果你使用Guzzle,那麼你可以這樣做:'print_r(json_decode((string)$ crawler-> getBody()));'。 – Sandeesh

回答

2

file_get_contents()函數確實像這些簡單的事情的伎倆。

function odeon(){ 
    $data = file_get_contents('https://api.cinelist.co.uk/get/times/cinema/10565'); 

    $data = json_decode($data); 
    return view()->make('odeon')->with(['listings' => $data->listings]); 

} 

,然後在你的刀片簡單地做這樣的事情:

@foreach($listings as $listing) 
    <strong>{{$listing->title}} </strong>: 
    @foreach($listing->times as $time) 
     <p>{{$time}}</p> 
    @endforeach 
@endforeach 
+0

看起來不錯,但是我需要將數據返回到刀片模板,所以我可以將它設計成div等 – Przemek

+0

那麼這不是你要求的。我會用更多的上下文來更新我的例子。 – larsemil

+0

函數odeon不返回視圖,那麼如何告訴它使用blade? – Przemek