日韩性视频-久久久蜜桃-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)容還不錯,歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: a∨鲁丝一区鲁丝二区鲁丝三区 | 依依成人综合 | 色眯眯影院 | 中文字幕国产日韩 | 激情综合网五月天 | 亚洲人成77777 | 91女人18毛片水多国产 | 国产精品女人久久久 | 国产夫妻性生活视频 | 91丨国产丨白丝 | 一级全黄裸体片 | 色婷婷av一区二区三区之红樱桃 | 亚洲淫片 | 国产精品一区二区三区久久久 | 性色av网址 | 国产精品suv一区二区三区 | 国产区久久 | 免费日本视频 | 亚洲欧美日韩中文字幕在线观看 | 亚洲一区网站 | 成人另类小说 | 国产成人免费观看视频 | 国产亚洲精品久久久久久 | 日韩福利视频导航 | 国内成人自拍视频 | 三男一女吃奶添下面 | 国产精品区二区三区日本 | 超碰女| 欧美激情二区三区 | 日日干日日爽 | 午夜三级网站 | 日本大尺度做爰呻吟舌吻 | 日本三级欧美三级 | 大陆av片 | 麻豆网站入口 | 成人性视频网站 | 欧美日韩伦理片 | 免费黄片毛片 | 九一亚色 | 日本国产一区 | 欧美性猛交xxxx乱大交3 | 国产a级免费视频 | 成人91在线| 日本无翼乌邪恶大全彩h | 亚洲精品av中文字幕在线在线 | 中文字幕99页 | 男人天堂va| 永久av网站 | aⅴ天堂网 | 精品国产免费视频 | 日日夜夜撸啊撸 | 免费成人深夜小野草 | 日本一区二区三区免费观看 | 国产福利资源 | 波多野结衣绝顶大高潮 | 国产成人在线播放视频 | 午夜男人网 | 俄罗斯精品一区二区三区 | 可以免费看av的网址 | 欧美黄色小视频 | 日本欧美日韩 | 国产一区二区三区四区五区六区 | 国产激情视频在线观看 | 伊人福利视频 | 欧美色图亚洲色 | 开心春色激情网 | 91.久久| 干美女av | 成人综合站 | 污污的网站在线观看 | 国模私拍一区二区三区 | 麻豆视频播放 | 思思99热| www.啪啪.com| 人人干人人做 | www视频在线观看网站 | 日本αv | 天天色影综合网 | 高清乱码免费看污 | 国产一级免费视频 | 久草资源在线观看 | 欧美激情国产一区 | 豆花在线观看 | 午夜视频在线 | 亚洲欧美日韩色 | 粉嫩av一区二区夜夜嗨 | 国产中出 | 亚洲一区国产 | 中文字幕欧美另类精品亚洲 | 韩国黄色av | 伊人福利视频 | 日韩美女视频在线观看 | 亚洲免费黄网 | 性久久久久久久久久 | 亚洲www在线 | 黄网站免费在线 | 丁香七月婷婷 | 无码无套少妇毛多18pxxxx | 免费荫蒂添的好舒服视频 |