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

发布于 2024-12-21 05:25:28 字数 819 浏览 0 评论 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>';
?>

I am trying to pull data from the justin.tv API and store the echo I get in the below code in to a database or a file to be included in the sidebar of website. I am not sure on how to do this. The example of what I am trying to achieve is the live streamers list on the sidebar of teamliquid.net. Which I have done but doing it the way I have done it slows the site way down because it does about 50 json requests every time the page loads. I just need to get this in to a cached file that updates every 60 seconds or so. Any ideas?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

护你周全 2024-12-28 05:25:28

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

}

I'm not entirely sure how you would imagine this being cached, but the code below is an adaption of a block of code I've used in the past for some Twitter work. There are a few things that could probably be done better from a security perspective. Anyway, this gives you a generic way of grabbing the Feed, parsing through it, and then sending it to the database.

Warning: This assumes that there is a database connection already established within your own system.

(* Make sure you scroll to the bottom of the code window *)

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文