日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php调用twitter api

發(fā)布時間:2025/3/8 php 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php调用twitter api 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

今天做了一個關(guān)于調(diào)用twitter api來同步twitter信息到自己網(wǎng)站的功能,雖然是比較簡單的功能,著實花費了我不少時間。網(wǎng)上有很多關(guān)于twitter api調(diào)用的方法,我試了幾個都沒有成功,貌似需要Oauth驗證,這個跟微信開發(fā)類似,需要token驗證。后來看到下面這篇博客解決了,講的挺詳細的。

主要步驟是:

第一步:到https://dev.twitter.com/apps/網(wǎng)站上創(chuàng)建一個app,獲取驗證需要的Consumer key, Consumer secret, Access token, Access token secret這四個參數(shù)。

第二步:引入twitteroauth進行驗證,這個twitteroauth可以到github上下載,源碼都是有的。最后按照給出的php代碼運行,調(diào)用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 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.

  • 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:

    01020304050607080910111213141516171819202122<?phpsession_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.


    轉(zhuǎn)載于:https://blog.51cto.com/php2013/1406250

    總結(jié)

    以上是生活随笔為你收集整理的php调用twitter api的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: 亚洲黄色片 | 伊人久久一区二区三区 | 热久久精品免费视频 | 黄色一级大片在线免费看国产 | 国产精品一二区 | 女性高潮视频 | 国产九一精品 | 精品视频在线观看一区二区 | 成人欧美一区二区三区黑人免费 | 91尤物视频在线观看 | 91秘密入口| 婷婷在线免费 | 一区影视 | 欧美成人毛片 | 四虎影视免费永久大全 | 精品免费在线视频 | 永久免费视频网站直接看 | 春色伊人| 亚洲精品四区 | 亚欧视频在线观看 | av动漫网站 | 精品午夜久久久 | 亚洲精品自拍视频 | 色多多视频污 | 国产人妻精品一区二区三区 | 亚洲视频一二三四 | 精品欧美乱码久久久久久1区2区 | 久久超碰精品 | 天天综合精品 | 91视频99| juliaann办公室丝袜大战 | 婷婷色激情 | 国产一区二区三区黄 | 亚洲一区二区三区免费看 | 国产视频久久久久久 | 艳妇乳肉豪妇荡乳xxx | 久久久久成人网站 | 不卡的av在线 | va欧美 | 高跟鞋调教—视频|vk | 国产精品免费一区二区 | 日韩高清在线观看一区 | 色综合av | 我们2018在线观看免费版高清 | 亚洲a图| 人人妻人人澡人人爽国产一区 | 少妇黄色一级片 | 日韩精品人妻中文字幕 | 特级西西444www高清大胆免费看 | 五月婷婷丁香久久 | 国产欧美一区二区 | 成人欧美一区二区三区黑人冫 | 91看片淫黄大片 | xxx国产| 美女免费黄视频 | 欧美性xxxxx极品娇小 | 国产污污在线观看 | 中国三级黄色 | 最新色网址| 国产精品一区在线观看你懂的 | 男女乱淫视频 | 男人的天堂一级片 | 韩毛片| 黄色免费入口 | 一级黄毛片| 久久久久久久极品内射 | 黄av资源| 毛片亚洲av无码精品国产午夜 | 免费91看片 | 永久免费的网站入口 | 免费黄色小视频在线观看 | 日本高清一区二区视频 | 免费大片在线观看www | 一区二区在线视频 | 亚洲av无码一区东京热久久 | 黄色的一级片 | 精品h| 污视频在线观看网址 | 日韩一区中文 | 欧美xxx视频 | 日本xx视频免费观看 | 美女视频网址 | 女女av在线| 特黄aaaaaa私密按摩 | 丁香七月激情 | 日韩一级视频在线观看 | 久久av导航 | 99香蕉视频 | 欧美日本黄色 | 114国产精品久久免费观看 | 猫咪av网 | 免费观看在线视频 | 国产又粗又猛视频免费 | 日韩高清在线观看一区 | www.欧美日韩 | 午夜视频| 免费av黄色 | 韩国一区二区在线播放 | 欧美专区日韩专区 |