今天做了一个关于调用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.

  1. 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.

  2. Select ‘Create new application’ and enter the application details.

    1. The name and description can be anything you like really, but you can’t use ‘Twitter’ in the name.

    2. The website field can be your main website and doesn’t have to be the site where your Twitter feed or feeds are located.

    3. Callback URL can be left blank

  3. Enter the CAPTCHA info and click create

  4. On the next details screen, click ‘create my access token’. You may need to refresh the page after a few seconds if it doesn’t appear automatically.

  5. Make a note of the Consumer key, Consumer secret, Access token and Access token secret as highlighted below.

Once you have an app setup within Twitter, this can be used for multiple user timelines on multiple websites – you do not need to setup one app per Twitter account or user timeline. Rate limits are set to 180 requests per 15 minute window however, per access token.

Step 2 – Authenticate the Twitter Feed

First off, head over to https://github.com/abraham/twitteroauth and download all the files. You’re only going to need to use a handful of these for this basic authentication but you might as well download the whole library. A key advantage of doing all this in PHP and recommended by Twitter is that your access tokens and keys are sent server side and not visible to the client.

Next, create a new php file, e.g. get-tweets1.1.php and use the following PHP code, substituting the 4 keys, twitter username and number of tweets you want to display. Upload this file along with the twitteroauth library to a folder on your web server and test the get tweets file.

Once you have an app setup within Twitter, this can be used for multiple user timelines on multiple websites – you do not need to setup one app per Twitter account or user timeline. Rate limits are set to 180 requests per 15 minute window however, per access token.

The PHP:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
session_start();
require_once("twitteroauth/twitteroauth/twitteroauth.php"); //Path to twitteroauth library
$twitteruser= "twitterusername";
$notweets= 30;
$consumerkey= "12345";
$consumersecret= "123456789";
$accesstoken= "123456789";
$accesstokensecret= "12345";
functiongetConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
$connection= newTwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
return$connection;
}
$connection= getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
$tweets= $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets);
echojson_encode($tweets);
?>

Hooray! you should have the latest tweets displaying in .json format. There should be a load of information displayed for each tweet within an array of information – it might look like a random mess but this is what you can now use to create a custom styled feed.

Check out my jQuery article if you want to create a custom twitter feed or my fading tweets post if you want to animate tweets one at a time. If you want to create a custom twitter search instead of a user timeline feed, see my Twitter search tutorial

You could of course get tweets to output direct from PHP as HTML but .json format and the articles above should be useful for anyone migrating from API V1 JavaScript Twitter feeds.

The above authentication code can also be easily modified for different Twitter endpoints, such as retrieving favorite tweets.

Troubleshooting tip 1: If you are getting internal 500 server errors on the twitter feed, this could be down to a number of things. Try enabling friendly PHP display errors or checking log files to see the exact error message. The most common mistake is an incorrect path to the twitteroauth.php file. Depending how the library is unzipped, it’s likely that the path will be “twitteroauth/twitteroauth/twitteroauth.php” or “twitteroauth/twitteroauth.php”. Use a relative path and not an absolute path, relative to where you put your get tweets file

Troubleshooting tip 2: Make sure you have cURL enabled on your server setup which is required by the Twitter OAuth library

Troubleshooting tip 3: If you’re getting a blank page, again, make sure you’re using a relative path to the OAuth library and ensure you have no HTML outside the opening and closing PHP

Troubleshooting tip 4: If you’re seeing a ‘null’ response, check the $connection->get call. I’ve seen null errors occur when trying to migrate old V1 calls that include a ‘callback’ parameter.

‘null’ responses when trying to authenticate Twitter also appear when using an earlier version of PHP – version 5.2.x. Try upgrading to a more recent version of PHP if possible or check your php.ini file and remove ‘curl_exec’ from ‘disable_functions’ if it exists. (thanks to Daniel Iftimie for this last one)

Troubleshooting tip 5: If the only thing you see is the get(); line and viewing page source shows the entire PHP, this means PHP isn’t activated on your server and your get tweets script isn’t being executed.

Note: The new rate limits for V1.1 for user timelines are 180 requests per 15 minute window. IP Address based limited no longer applies as it did with non-authenticated requests. So if you have a high volume of visitors to your website, or want to use the same access tokens across multiple sites and different twitter feeds then it’s probably worth setting up scheduled caching of tweets.

转载于:https://blog.51cto.com/php2013/1406250

php调用twitter api相关推荐

  1. twitter php_php调用twitter api

    今天做了一个关于调用twitter api来同步twitter信息到自己网站的功能,虽然是比较简单的功能,着实花费了我不少时间.网上有很多关于twitter api调用的方法,我试了几个都没有成功,貌 ...

  2. 不通过twitter API获取Twitter数据的方法

    不通过twitter API调用Twitter数据 前言 1.Python库的导入 2.网络问题的解决 2.1.可能遇到的错误 2.1.1.Cannot Connect to host twitter ...

  3. 推特php博客,实例代码:使用PHP调用Twitter的RSS

    "守望轩"博客右侧边栏原来有个"杂感"的栏目,用来记录短的.不能大篇幅成文的短句,或者自己比较喜欢的短句和言论. 这个栏目最开始调用微博饭否的API来做的,因为 ...

  4. twitter APi的使用与twitter数据的应用

    网络爬虫: 网络爬虫是一种按照一定的规则,自动的抓取万维网信息的程序或者脚本,是搜索引擎的重要组成.传统爬虫从一个或若干初始网页的URL开始,获得初始网页上的URL,在爬取网页的过程中,不断从当前页面 ...

  5. Python 使用 twitter API 获取twitter用户信息

    Python 使用 twitter API 获取twitter用户信息 1. 概述 twitter作为国外极其大众化的社交平台,具有大量的海外用户,平台流动数据量极大,是国外人群生活数据的重要来源之一 ...

  6. 【Twitter API开发者账户协议必知】

    Twitter API协议必知 中文 英文 中文 开发商协议 生效日期:2022年10月10日 本Twitter开发者协议(以下简称"协议")由您(个人或实体,以下简称" ...

  7. 【笔记】使用Twitter API V2进行数据爬取的经验总结

    写在最前面:这篇笔记主要是基于自己使用API过程中遇到的问题以及不断的尝试形成的经验总结,所有内容都是一个字一个字敲的,所以还挺辛苦的.不过也正是因为这只是一些个人经验的归纳,所以对于API的函数和功 ...

  8. python 图表_Python入门学习系列——使用Python调用Web API实现图表统计

    使用Python调用Web API实现图表统计 Web API:Web应用编程接口,用于URL请求特定信息的程序交互,请求的数据大多以非常易于处理的格式返回,比如JSON或CSV等. 本文将使用Pyt ...

  9. C#中调用Windows API时的数据类型对应关系

    C#中调用Windows API时的数据类型对应关系 原文 C#中调用Windows API时的数据类型对应关系 BOOL=System.Int32 BOOLEAN=System.Int32 BYTE ...

最新文章

  1. 01《软件需求分析教程》
  2. Unable to lock JVM Memory: error=12--elasticsearch
  3. 【必备】jQuery性能优化的38个建议
  4. server 群辉emby_群晖容器中搭建支持硬件编码的Emby Server教程及注意事项
  5. Qt文档阅读笔记-The Meta-Object System解析及实例
  6. c语言单片机串口通讯,单片机C语言之串口通信协议
  7. mysql timestamp 当前_技术分享 | MySQL 复制那点事 - Seconds_behind_Master 参数调查笔记
  8. 沃达丰V1210刷机教程
  9. 身份证识别+银行卡识别:开启移动互金新体验
  10. Nagios利用NRPE监控Linux主机
  11. ubuntu16.04安装nvidia-384
  12. sqlserver 修改表字段长度
  13. UML基础(八)--构件图
  14. Keil安装(带安装软件)
  15. 《给忙碌者的天体物理学》pdf、mobi、epub下载
  16. 看看绝地求生—PUBG的游戏模型是怎么出来的!
  17. snapchat_如何截屏和共享Snapchat快照
  18. 使用certbot为nginx站点添加免费ssl证书
  19. 什么是外包公司,外包公司与互联网公司的区别
  20. 该为您的HTTPS安全证书续期了

热门文章

  1. oracle空例程,2018.5.29 Oracle连接到空闲例程
  2. 修改域服务器IP,域控制器迁移以及修改服务器ip
  3. cd linux给u盘安装程序,使用U盘安装CDlinux
  4. eclipse中配置jad反编译插件
  5. 在linux实现公平队列,多级反馈队列调度策略在Linux中的应用和实现.pdf
  6. C语言超级玛丽菜单模块源码
  7. java获取类的信息
  8. 菱形继承和虚继承、对象模型和虚基表
  9. c++进制转换_一文了解进制之间的原理和转换
  10. 对Docker镜像layer的理解