今天做了一个关于调用twitter api来同步twitter信息到自己网站的功能,虽然是比较简单的功能,着实花费了我不少时间。网上有很多关于twitter api调用的方法,我试了几个都没有成功,貌似需要Oauth验证,这个跟微信开发类似,需要token验证。后来看到下面这篇博客解决了,讲的挺详细的。
主要步骤是:
第一步:到https://dev.twitter.com/apps/网站上创建一个app,获取验证需要的Consumer key, Consumer secret, Access token, Access token secret这四个参数。
第二步:引入twitteroauth进行验证,这个twitteroauth可以到github上下载,源码都是有的。最后按照给出的php代码运行,调用get或post方法抓取,这里是用到的是curl,twitteroauth里封装了http这样一个方法,里面用curl进行抓取。
Step 1 – Setup a Twitter Application
This process is straightforward and you should have a set of keys within a few minutes.Visit https://dev.twitter.com/apps/ and sign in using your Twitter username and password. This doesn’t have to be the username or password of the stream you need access to, just a Twitter account you control.
Select ‘Create new application’ and enter the application details.The name and description can be anything you like really, but you can’t use ‘Twitter’ in the name.
The website field can be your main website and doesn’t have to be the site where your Twitter feed or feeds are located.
Callback URL can be left blank
Enter the CAPTCHA info and click create
On th