0

我正在尝试从 justin.tv API 中提取数据,并将我在以下代码中获得的回声存储到数据库或要包含在网站侧边栏中的文件中。我不确定如何做到这一点。我想要实现的示例是 teamliquid.net 侧边栏上的直播流媒体列表。我已经这样做了,但是按照我的做法这样做会减慢站点速度,因为每次加载页面时它都会执行大约 50 个 json 请求。我只需要将它放入每 60 秒左右更新一次的缓存文件中。有任何想法吗?

<?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>';
?>
4

1 回答 1

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

}
于 2011-12-14T02:23:09.423 回答