2011-12-14 10 views
0

私はjustin.tv APIからデータを取得し、以下のコードをデータベースまたはウェブサイトのサイドバーに含めるファイルに保存しようとしています。私はこれを行う方法についてはわかりません。私が達成しようとしているのはteamliquid.netのサイドバーにあるライブストリーマーリストです。私がやったことですが、私がやったやり方でやってみると、ページが読み込まれるたびに約50回のjsonリクエストが行われるため、サイトの速度が遅くなります。私はちょうど60秒程度ごとに更新されるキャッシュされたファイルにこれを入れる必要があります。何か案は?ファイルまたはデータベースに保存するためにjson APIを使用する

<?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の仕事のために過去に使用したコードのブロックの適応です。セキュリティの観点から、おそらくもっとうまくいく可能性のあることがいくつかあります。とにかく、これはフィードを取得し、それを解析し、それをデータベースに送信する一般的な方法を提供します。

警告:これは、自分のシステム内にすでにデータベース接続が確立されていることを前提としています。

*あなたがコードウィンドウ*の一番下までスクロールしていることを確認してください)

/** 
* 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

+0

混乱を招待して申し訳ありません。上のコードまたはGistのコードをhttps://gist.github.com/1478159にコピーして、PHPファイルとして保存してください。 MySQLの一部についても助けが必要な場合は、私に知らせてください。 –

+0

これは正確に何をするのですか?上記の投稿に私が持っているエコーが保存されていますか?基本的に私はjsonが彼が "生きている"と答えた場合は、自分のウェブサイトのサイドバーのリストに "col.Catz"と言ってほしい。私はチェックして表示する必要がある約50ストリーマーがあります。 http://www.teamliquid.netのサイドバーにある「ライブストリーム」の領域を参照して、私がやりたいことの例を示しますが、ロードするたびにウェブサイトを遅くしないようにしてください。 – user1096935

関連する問題