使用json API pull存储在文件或数据库中



我正试图从数据库中提取数据。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>';
?>

我不完全确定您会如何想象它被缓存,但是下面的代码是我过去在一些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
}

相关内容

  • 没有找到相关文章

最新更新