Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8319945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:29:16+00:00 2026-06-08T22:29:16+00:00

I have a twitter feed on my website which is powerd by the jquery

  • 0

I have a twitter feed on my website which is powerd by the jquery tweet plugin tweet.seaofclouds.com and some other json plugins that take a json feed from a 3rd party website.

The problem is dat the twitter api only allows 150 calls per hour, so with 40 visitors an hour with an average of 5 pageviews per visitor I go way past that max. Especially since twitter disabled caching on the feed.

Also, there is the coockie law problem. Twitter drops cookies when the feed is requested and I don’t want them because they need to have permission to be dropped, so I want to disable them all the way.

Also my website is SSL secure and I want to load as minimal external resources as possible, I want it all localised.

How do I locally cache these json feeds?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T22:29:18+00:00Added an answer on June 8, 2026 at 10:29 pm

    For this problem I have written my own database storing mechanism to store the json feeds and fetch them when needed and return them. That way I only need to fetch every 5 minutes and the amount of visitors/pageviews I get is irrelevant.

    Here is the database creation code in 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;
    

    Then in PHP I have this code with some security checks since you’ll never know what you’ll get

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

    Then in the twitter.js you only need to replace the getJSON url to point to your local server as follows(somewhere at the bottom of jquery.tweet.js you’ll find this line)

    Find: $.getJSON(build_api_url()).success(function(data){

    Replace:

    // 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){
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Anyone have an pointers on an easy way to consume a search.twitter.com feed with
I have a twitter feed in the format: 1. Username: Blah blah http://something.com #hashtag
I have a web site at http://www.urbanvision.org.uk/ , we have a Twitter feed on
I have the following function that retrieves an image from a twitter feed, the
I have multiple twitter share buttons on a page, I want to identify which
I have been using Twitter's Bootstrap Tooltip plugin . This works perfectly fine except
I have a twitter feed and I create a new date obj so I
I have just coded twitter feed for my statuses fetching the xml. I was
I have am reading a twitter feed in my iPhone application and can do
I want to build an android-based twitter feed reader app. I have problems in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.