2011-12-14 74 views
0

我想從justin.tv API中提取數據並將下面的代碼中獲得的回聲存儲到數據庫或文件中以包含在網站的側邊欄中。我不知道如何做到這一點。我試圖實現的例子是teamliquid.net邊欄上的實時流表列表。我已經完成了這些工作,但是按照我這樣做的方式進行,因爲它每次加載頁面時都會執行大約50次json請求。我只需要將它存入一個每60秒更新一次的緩存文件中。有任何想法嗎?使用json API pull來存儲在文件或數據庫中

<?php 
$json_file = file_get_contents("http://api.justin.tv/api/stream/list.json?channel=colcatz"); 
$json_array = json_decode($json_file, true); 

if ($json_array[0]['name'] == 'live_user_colcatz') echo '<a href="http://www.twitch.tv/colcatz">coL.CatZ</a> Live<br>'; 

$json_file = file_get_contents("http://api.justin.tv/api/stream/list.json?channel=coldrewbie"); 
$json_array = json_decode($json_file, true); 

if ($json_array[0]['name'] == 'live_user_coldrewbie') echo '<a href="http://www.twitch.tv/coldrewbie">coL.drewbie</a> Live<br>'; 
?> 

回答

1

我不完全相信你會如何想象這種被緩存,但下面的代碼是我在過去使用的一些Twitter的工作代碼塊的適應。從安全角度來看,有幾件事情可能會更好地完成。無論如何,這給你一個通用的方式來抓取Feed,解析它,然後將它發送到數據庫。

警告:這裏假設您的系統中已經建立了數據庫連接。

*請確保您滾動到代碼窗口*的底部)

/** 
* Class SM 
* 
* Define a generic wrapper class with some system 
* wide functionality. In this case we'll give it 
* the ability to fetch a social media feed from 
* another server for parsing and possibly caching. 
* 
*/ 

class SM { 

    private $api, $init, $url; 

    public function fetch_page_contents ($url) { 

    $init = curl_init(); 

    try { 
     curl_setopt($init, CURLOPT_URL, $url); 
     curl_setopt($init, CURLOPT_HEADER, 0);  
     curl_setopt($init, CURLOPT_RETURNTRANSFER, 1);  
    } catch (Exception $e) { 
     error_log($e->getMessage()); 
    } 

    $output = curl_exec($init); 
    curl_close($init); 

    return $output; 
    } 

} 


/** 
* Class JustinTV 
* 
* Define a specific site wrapper for getting the 
* timeline for a specific user from the JustinTV 
* website. Optionally you can return the code as 
* a JSON string or as a decoded PHP array with the 
* $api_decode argument in the get_timeline function. 
* 
*/ 
class JustinTV extends SM { 

    private $timeline_document, 
      $api_user, 
      $api_format, 
      $api_url; 

    public function get_timeline ($api_user, $api_decode = 1, $api_format = 'json', $api_url = 'http://api.justin.tv/api/stream/list') { 

    $timeline_document = $api_url . '.' . $api_format . '?channel=' . $api_user; 

    $SM_init = new SM(); 

    $decoded_json = json_decode($SM_init->fetch_page_contents($timeline_document)); 

    // Make sure that our JSON is really JSON 
    if ($decoded_json === null && json_last_error() !== JSON_ERROR_NONE) { 
     error_log('Badly formed, dangerous, or altered JSON string detected. Exiting program.'); 
    } 

    if ($api_decode == 1) { 
     return $decoded_json; 
    } 

    return $SM_init->fetch_page_contents($timeline_document); 
    } 

} 


/** 
* Instantiation of the class 
* 
* Instantiate our JustinTV class, fetch a user timeline 
* from JustinTV for the user colcatz. The loop through 
* the results and enter each of the individual results 
* into a database table called cache_sm_justintv. 
* 
*/ 
$SM_JustinTV = new JustinTV(); 

$user_timeline = $SM_JustinTV->get_timeline('colcatz'); 

foreach ($user_timeline AS $entry) { 

    // Here you could check whether the entry already exists in the system before you cache it, thus reducing duplicate ID's 

    $date = date('U'); 

    $query = sprintf("INSERT INTO `cache_sm_justintv` (`id`, `cache_content`, `date`) VALUES (%d, '%s',)", $entry->id, $entry, $date); 
    $result = mysql_query($query); 

    // Do some other stuff and then close the MySQL Connection when your done 

} 
+0

很抱歉,但我不知道如何實現這個,甚至它做什麼。 – user1096935 2011-12-14 03:48:03

相關問題