我有一個網站與用戶的數據庫,我想讓他們能夠顯示他們的個人資料頁面上他們當前的鳴叫消息(如果他們有一個Twitter帳戶)。分享上一個網頁的最後一個鳴叫
搜索後,我看到了這個工具:http://juitter.com 這似乎是隻是我需要有點複雜。
我正在使用Rails。你知道一個儘可能簡單的工具可以做到嗎?
謝謝 d
我有一個網站與用戶的數據庫,我想讓他們能夠顯示他們的個人資料頁面上他們當前的鳴叫消息(如果他們有一個Twitter帳戶)。分享上一個網頁的最後一個鳴叫
搜索後,我看到了這個工具:http://juitter.com 這似乎是隻是我需要有點複雜。
我正在使用Rails。你知道一個儘可能簡單的工具可以做到嗎?
謝謝 d
你可以看看Twitter的API:users/show
例子:
$ curl http://twitter.com/users/show/20536157.json
{"notifications":false,"time_zone":"Pacific Time (US &
Canada)","friends_count":214,
"profile_sidebar_border_color":"bbccff","url":"http://www.google.com/support/",
"description":"News and updates from Google","status":{"created_at":"Mon Jan
11 19:38:40 +0000
2010","source":"web","truncated":false,"favorited":false,
"in_reply_to_user_id":null,
"in_reply_to_status_id":null,"in_reply_to_screen_name":null,"id":7640306427,
"text":"If
you're interested in what's happening with @google in Sub-Saharan Africa,
check out the new @googleafrica for news &
info."},
"geo_enabled":false,"favourites_count":73, "created_at":"Tue Feb 10
19:14:39 +0000 2009","profile_text_color":"000000","verified":false,
...
}
你可以得到最後的鳴叫用簡單的jQuery電話:
<!DOCTYPE html>
<html>
<head>
<script src="http://www.google.com/jsapi"></script>
<script>google.load("jquery", "1");</script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
$.getJSON("http://twitter.com/users/show/20536157.json",
function(data){ $("#tweet").text(data.status.text); });
});
</script>
</head>
<body>
<div id="tweet"><!-- last status will show up here --></div>
</body>
</html>
mikus答案沒爲我工作,我得到了
XMLHttpR equest無法加載http://twitter.com/users/show/ditact.json。 Access-Control-Allow-Origin不允許原產地http://mydomain.com。
我發現Accessing JSON service from localhost or file://
的一種方式回答,以繞過同源策略是加載JSONP而不是JSON。 你可以使用twitter API和jQuery來做到這一點,你只需要添加「callback =?」網址。
<!DOCTYPE html>
<html>
<head>
<script src="http://www.google.com/jsapi"></script>
<script>google.load("jquery", "1");</script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
// vvvvvvvvvvv
$.getJSON("http://twitter.com/users/show/20536157.json?callback=?",
// ^^^^^^^^^^^
function(data){ $("#tweet").text(data.status.text); });
});
</script>
</head>
<body>
<div id="tweet"><!-- last status will show up here --></div>
</body>
</html>
非常感謝!只是最後一個問題:我怎樣才能得到配置文件的id號? (例如來自http://twitter.com/yukihiro_matz) – Denis 2010-01-12 11:24:47
看看頁面的html源代碼。有幾個對用戶標識的引用,例如'http://twitter.com/statuses/user_timeline/20104013.rss'。我沒有深入研究這個問題,但我會建議你的用戶直接提供他們的推特ID,或者你一次抓取頁面以獲取用戶ID並將其存儲在本地服務器上。這只是一個快速回復..你可能會找到更好的方法來實現這個.. – miku 2010-01-12 11:33:42