Caching API Twitter calls for Twitter Profile Widg

2020-07-29 17:46发布

问题:

I'm developing a site locally, the site uses the Twitter Profile Widget twice, for two separate twitter feeds.

I've come up against the Rate Limit (150 calls / per hour) a few times now and i've been looking at ways to manage this.

One technique i've found uses Cron Jobs to call a PHP file that caches the Twitter API call every 10 minutes, saving it to a txt file on the server and parsing it using JQuery. Steps 2,3,5.. http://net.tutsplus.com/tutorials/php/how-to-create-an-advanced-twitter-widget/

My question is.. how would i then parse the JSON contents into my Twitter Profile Widget?

I suspect this might not be possible..

below is the standard Twitter Profile Widget code used to display the twitter feeds.

<script charset="utf-8" src="http://widgets.twimg.com/j/2/widget.js"></script>
            <script>
                new TWTR.Widget({version: 2, type: 'faves', rpp: 10, interval: 30000, title: ' ',subject: ' ', width: 210, height: 330,
                  theme: {
                    shell: {
                      background: ' ',
                      color: '#ffffff'
                    },
                    tweets: {
                      background: '#ffffff',
                      color: '#444444',
                      links: '#53cdc7'
                    }
                  },
                  features: {
                    scrollbar: true,
                    loop: false,
                    live: true,
                    behavior: 'all'
                  }
                }).render().setUser('diariesdownundr').start();

                new TWTR.Widget({version: 2,type: 'faves',rpp: 10,interval: 30000,title: ' ',subject: ' ',width: 210,height: 330,
                  theme: {
                    shell: {
                      background: ' ',
                      color: '#ffffff'
                    },
                    tweets: {
                      background: '#ffffff',
                      color: '#444444',
                      links: '#53cdc7'
                    }
                  },
                  features: {
                    scrollbar: true,
                    loop: false,
                    live: true,
                    behavior: 'all'
                  }
                }).render().setUser('diaries1').start();
            </script>

回答1:

This is indeed possible. Grab the uncompressed widget.js from http://twitter.com/javascripts/widgets/widget.js, and change var profileBase (around line 884) to read from the cache file on your site instead of api.twitter.com. Load your new widjet.js instead of the one you're loading now from widgets.twimg.com

As for the actual 'twitter cache' file to read from - using a cronjob is indeed one way to do it. When I did this, I used curl to make the exact same request to api.twitter.com that the current widget.js was making - it worked if I grabbed the entire URL of the request made by widget.js, along with all the GET parameters (you can see this in Firebug, for example), threw it to curl (so, curl_init($url) and curl_exec($url) ), and wrote the output of curl_exec into a cache file.