2015-10-20 92 views
0

我在编程专门laravel新如何从其他网站获取数据?

这是我要显示我想从.... 获取数据的链接,这是一个运动列表

[http://www.lionpubpattaya.com/index.php/tv-guide][1]

数据在我的应用程序,并应显示像这样

JSON 
{ 

"games":[ 

{ 

"gameTitle":"PREMIER LEAGUE FOOTBALL", 

"gameList" : { 

"0" : "Sat 15 - 1845 SOUTHAMPTON VS EVERTON", 

"1" : "Sat 15 - 2100 SUNDERLAND VS NORWICH", 

"2" : "Sat 15 - 2100 SWANSEA VS NEWCASTLE", 

"3" : "Sat 15 - 2100 TOTTENHAM VS STOKE", 

"4" : "Sat 15 - 2100 WATFORD VS WEST BROM", 

"5" : "Sat 15 - 2100 WEST HAM VS LEICESTER", 

"6" : "Sun 16 - 1930 CRYSTAL PALACE VS ARSENAL" 

} 

}, 

{ 

"gameTitle":"NRL", 

"gameList" : { 

"0" : "Thurs 13 - 1630 COWBOYS VS RABBITOHS", 

"1" : "Fri 14 - 1630 BRONCOS VS DRAGONS", 

"2" : "Sat 15 - 1155 WEST TIGERS VS KNIGHTS", 

"3" : "Sat 15 - 1425 PANTHERS VS WARRIORS", 

"4" : "Sat 15 - 1625 ROOSTERS VS EELS", 

"5" : "Sun 16 - 1100 RAIDERS VS SEA EAGLES", 

"6" : "Sun 16 - 1300 BULLDOGS VS TITANS", 

"7" : "Mon 17 - 1600 SHARKS VS STORM" 

} 

}, 

{ 

"gameTitle":"AFL", 

"gameList" : { 

"0" : "Fri 14 - 1630 SYDNEY VS COLLINGWOOD", 

"1" : "Sat 15 - 1030 ESSENDON VS ADELAIDE", 

"2" : "Sat 15 - 1110 KANGAROOS VS ST KILDA", 

"3" : "Sat 15 - 1330 PT ADELAIDE VS GW SYDNEY", 

"4" : "Sat 15 - 1620 GEELONG VS HAWTHORN", 

"5" : "Sat 15 - 1620 BRISBANE VS CARLTON", 

"6" : "Sun 16 - 1000 RICHMOND VS GOLD COAST", 

"7" : "Sun 16 - 1220 BULLDOGS VS MELBOURNE", 

"8" : "Sun 16 - 1330 FREMANTLE VS WEST COAST" 

} 

} 




this is my code in my laravel controller 

    public function index() 
    { 

     // $file = file_get_contents("excel/sportstv.txt"); 
     // return $file; 
     libxml_use_internal_errors(true); 
     $html = file_get_contents("http://www.lionpubpattaya.com/index.php/tv-guide"); 

     $dom = new \DOMDocument(); 
     $dom->loadHTML($html); 
     $nodes = $dom->document.getElementById('<h3>'); 
     $result = array(); 
     foreach ($nodes as $node) { 
      $result = $node->nodeValue; // return <h3> tag data 
     } 
     $response = response()->json(array("Title",$result)); 

     return $response; 
     libxml_use_internal_errors(true); 
    } 

回答

0

我已经解决了我的问题。这是答案。

$url = "http://www.lionpubpattaya.com/index.php/tv-guide"; 
$html = file_get_html ($url); 

$games = []; 
$i = 0; 
foreach ($html->find ('h2') as $element) { 
    $games[$i]['gameTitle'] = trim(str_replace("\r\n","", $element->plaintext)); 
    $games[$i]['gameList'] = explode("\r\n", $element->next_sibling()->plaintext); 
    $i++;   
    flush(); 
} 

unset($games[15]); 
unset($games[16]); 

$games = array_filter($games); 

return $games;