许多网站和相关初学者的Twitter API 1.1,OAuth和PHP(用于内容管理系统)

At the moment we query a Twitter user's timeline using file_get_contents to get a JSON feed which we cache for a minute at a time and life was good. However, after some of our hundreds of sites started to break (when Twitter helpfully started removing the old XML urls), I noticed they were going to be changing to API 1.1 next year and we'd need to authenticate requests.

So, I'm basically looking for a simple pointer in the right direction. So far I assume the following:

  1. The new GET resource seems quite similar, bonus!
  2. Since we're only ever displaying that user's tweets, we're going to need only a dev.twitter.com token
  3. It seems like we're going to need to create a new token on that user's account for every website we do.
  4. We probably have to install a library similar to twitteroauth as found https://dev.twitter.com/docs/auth/oauth/single-user-with-examples#php

As I say, we're simply displaying the user's tweets who owns the website we're building. People don't log in or post or anything like that.

I know there are embedded timelines, but these afford almost no customisation and I already hate Facebook's 'Like Box' for the same reason. (but I also know Twitter's 'Display Requirements' offer little to no flexibility other than probably colours...

So, taking that into account, is this the easiest way to do it? It seems relatively straight forward (I mean twitteroauth does the get request in 7 lines or less once set up), the biggest pain sounds like having to talk each user through getting an access key.

Thanks.

Twitteroauth is certainly going to give you a head start on this. Remember though in the twitter documentation you are only allowed to do 180 timeline request in 15 minutes and if you go above that they will limit you for about an hour I think (it could be less). You should also read the bit about timelines if you want to be able to show every tweet you most not what the last ID you got was. Of course if you do all of this in realtime then your website will slow down as it makes the request to twitter.

I wouldn't use file_get_contents, mostly because it is slightly slower than curl and twitteroauth provides a much nicer interface IMO, less coding for you and it's forkable in git.

So, yes cache your results and keep an eye on how many requests you make within the 15 minutes allocated in 1.1. If you go over you will regret it for an hour, and that applies to every app using your key.

Hope that helps