【问题标题】:Using json API pull to store in file or database使用 json API pull 存储在文件或数据库中
【发布时间】:2012-01-19 21:12:44
【问题描述】:

我正在尝试从 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>';
?>

【问题讨论】:

    标签: json list streaming justin.tv


    【解决方案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
    
    }
    

    【讨论】:

    • 抱歉,我不知道如何实现它,甚至不知道它的作用。
    • 抱歉添加了混淆。如果从上面复制代码或从此处gist.github.com/1478159 的要点复制代码,然后将其保存为 PHP 文件,您可以运行该程序。如果您也需要有关 MySQL 部分的帮助,请告诉我。
    • 那么这到底是做什么的呢?它是否存储了我在上述帖子中的回声?基本上我希望它在我网站侧边栏的列表中说“col.Catz”,如果json返回他是“活的”,如果他不是。我需要检查和显示大约 50 个流媒体。请参阅teamliquid.net 侧边栏上的“实时流”区域,了解我想要做的示例,但这样做不会在每次加载时减慢网站速度。
    猜你喜欢
    • 2021-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多