loading..

[Code] Hiển thị Entry mới nhất từ Twitter

by Huỳnh Trung on January 14, 2010

Có nhiều cách để lấy Entry từ Tiwtter. Ở WP cũng có rất nhiều plugin thực hiện việc này. Tuy nhiên, nếu chỉ đơn giản muốn lấy entry mới nhất từ twitter tại sao chúng ta không tự viết code để lấy nhỉ. Hạn chế dùng plugin. Bắt đầu nhé :D .

Ở đây HuTruc hướng dẫn tạo ra một file php để lấy nội dung thôi. Còn việc sử dụng thế nào tuỳ các bạn nhé :P

Tạo một file mới với tên twit.php (tên gì cũng được :P ). Copy đoạn code sau và Paste vào. Up lên là xong.

<?php
// Your twitter username.
$username = "hutruc"; //Tai khoan twitter cua ban
$prefix = ""; // No se hien thi truoc Twitter entry
$suffix = ""; //No se hien thi sau Twitter entry
$feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=1"; //Lay ra noi dung duoi dang rss
function parse_feed($feed) {
$stepOne = explode("<content type="html">", $feed); //Loc noi dung
$stepTwo = explode("</content>", $stepOne[1]); //Loc noi dung
$tweet = $stepTwo[0]; //Loc noi dung
$tweet = str_replace('&lt;', '<', $tweet);//Loc noi dung
$tweet = str_replace('&gt;', '>', $tweet);//Loc noi dung
$tweet = str_replace('&quot;', '"', $tweet);//Loc noi dung
$tweet = str_replace('">', '" target="_blank" title="HuTrc" rel="nofollow">', $tweet);//Loc noi dung
return $tweet; //Tra ve
}
$twitterFeed = file_get_contents($feed); //Lay ra noi dung
echo stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix); //In ra ket qua
?>

Chúc bạn thành công :vic: .


Leave a Comment

CommentLuv Enabled
:cuoiduyen: :2: :$$$: :potoanthan: :heya: :bi: :bimat: :nangly: :D :-O :chem: more »

Previous post:

Next post: