Php twitterapi每小时只允许150个json调用,Twitter会丢弃不需要的cookie。我如何防止这种情况?

Php twitterapi每小时只允许150个json调用,Twitter会丢弃不需要的cookie。我如何防止这种情况?,php,jquery,json,caching,Php,Jquery,Json,Caching,我的网站上有一个推特提要,它由jquery推特插件tweet.seaofclouds.com和其他一些json插件驱动,这些插件从第三方网站获取json提要 问题是,twitter api每小时只允许150次调用,因此每小时有40个访问者,平均每个访问者有5次页面浏览,我远远超过了这个最大值。特别是因为twitter禁用了提要上的缓存 此外,还有库奇法问题。Twitter在请求订阅源时会删除cookies,我不想要它们,因为它们需要有删除权限,所以我想一路禁用它们 此外,我的网站是SSL安全的,

我的网站上有一个推特提要,它由jquery推特插件tweet.seaofclouds.com和其他一些json插件驱动,这些插件从第三方网站获取json提要

问题是,twitter api每小时只允许150次调用,因此每小时有40个访问者,平均每个访问者有5次页面浏览,我远远超过了这个最大值。特别是因为twitter禁用了提要上的缓存

此外,还有库奇法问题。Twitter在请求订阅源时会删除cookies,我不想要它们,因为它们需要有删除权限,所以我想一路禁用它们

此外,我的网站是SSL安全的,我想加载尽可能少的外部资源,我希望它都本地化


如何在本地缓存这些json提要?

对于这个问题,我编写了自己的数据库存储机制来存储json提要,并在需要时获取它们并返回它们。那样的话,我只需要每5分钟取一次,我得到的访问者/页面浏览量是无关紧要的

下面是mysql中的数据库创建代码

CREATE TABLE IF NOT EXISTS `twitterbackup` (
  `url` text NOT NULL,
  `tijd` int(11) NOT NULL,
  `inhoud` text NOT NULL,
  FULLTEXT KEY `url` (`url`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
然后在PHP中,我有一些安全检查的代码,因为你永远不知道你会得到什么

<?php
/* JSON Backup script written by Michael Dibbets
 * Copyright 2012 by Michael Dibbets
 * http://www.facebook.com/michael.dibbets - mdibbets[at]outlook.com
 * Licenced under the MIT license http://opensource.org/licenses/MIT
 */
// Basic sql injection protection.
// Using explode because str_replace fails to many times when certain character combinations exist. 
// Replace, remove as you see fit. This setup works for my server, and str_replace works on another. 
// Use whatever has your fancy
function protect($s)
    {
    $s = mysql_real_escape_string($s);
    $s = implode(" ",explode(";",$s));
    $s = implode(" ",explode("UNION",$s));
    $s = implode(" ",explode("BENCHMARK",$s));
    $s = implode(" ",explode("WAITFOR DELAY",$s));
    $s = implode(" ",explode("LOAD_FILE",$s));
    $s = implode(" ",explode("OUTFILE",$s));
    $s = implode(" ",explode("INFORMATION_SCHEMA",$s));
    $s = implode(" ",explode("Char(",$s));
    $s = implode(" ",explode("CAST(",$s));
    return $s;
    }
function get_data($url)
    {
    // Initialise data to have at least something to work with
    $data = "";
        // What time is it?
        $now = strtotime("now");
        // Connect to our database
        $db = mysqli_connect("localhost", "USERNAME", "PASSWORD", "DATABASE");
        if (mysqli_connect_errno($mysqli)) 
            {
            die("ARGH!");
            }
        // Basic protection agains sql injection by banning unsafe words
        $saveurl = protect($url);
        // Count how many times the url has been found.
        $count = $db->query("SELECT count(*) as counter FROM twitterbackup WHERE `url`='$saveurl'")->fetch_assoc();
        // Has the url been found?
        if($count['counter'] == 0)
            {
            // Fetch twitter json 
            $ch = curl_init();
            $timeout = 5;
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch,CURLOPT_URL,$url);
            curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
            $data = curl_exec($ch);
            curl_close($ch);
            // make the json data database safe
            $data = str_replace('\\','\\\\',$data);
            $data = mysql_real_escape_string($data);
            //$data = mysql_real_escape_string($data);
            // Enter json data in the database
            $db->query("INSERT INTO `DATABASE`.`twitterbackup` (`url`, `tijd`, `inhoud`) VALUES ('$saveurl', '$now', '$data')");
            // End of we have not found the url
            }
        // If the URL has been found
        else
            {
            // get the values in the database that are connected to the url
            $res = $db->query("SELECT * FROM twitterbackup WHERE `url`='$saveurl'")->fetch_assoc();
            // Is the current json in database younger than five minutes?
            if((int)$res['tijd'] < (int)strtotime("-5 minutes"))
                {
                // Fetch twitter json with curl
                $ch = curl_init();
                $timeout = 5;
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($ch,CURLOPT_URL,$url);
                curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
                curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
                $data = curl_exec($ch);
                curl_close($ch);
                // Make the json data safe for the database
                $data = str_replace('\\','\\\\',$data);
                $data = mysql_real_escape_string($data);
                // Update the database with the most recent feed
                $db->query("UPDATE  `DATABASE`.`twitterbackup` SET 
                            `tijd` =  '$now',
                            `inhoud` =  '$data' 
                            WHERE  `twitterbackup`.`url` =  '$saveurl'");
                // End if the url has been found and lifetime is older than five minutes
                }
            // If the lifetime isn't older then 5 minutes
            else
                {
                // return database content
                $data = $res['inhoud'];
                }
            // end of if we have found the url
            }
          // try to beat mysql_real_escape_string to return valid json. Always check valid json returend and edit this if it fails at some piece. Try http://jsonlint.com/
          $data = str_replace('\\"','"',$data);
          // implode because str_replace won't do everthing for some reason I can't understand.
          $data = implode('\\',explode('\\\\',$data));
          // Data retourneren
          return $data;
        // end check if it's from the twitter api
        }
// End of function get_data();
// How long may the json be cached by browser(to stop unneccesary requests in this case 5 minutes)
$seconds_to_cache = 5*60;
$ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
header("Expires: $ts");
header("Pragma: cache");
header("Cache-Control: max-age=$seconds_to_cache");
header('Content-type: application/json');
echo get_data($_GET['url']);
?>
// For debug purposes
// console.log("/scripts/twitter/tweet/curlthispage.php?url="+encodeURIComponent(build_api_url()));
        $.getJSON("/scripts/twitter/tweet/curlthispage.php?url="+encodeURIComponent(build_api_url())).success(function(data){