2011-06-19 73 views
-6

我可以问我如何在我的网站上显示我的推文?就像Twitter的饲料thingie。我想要一张桌子,里面只会显示我的Twitter帐户的推文。多谢你们!Twitter网站的推送

+0

看这个问题的http://计算器.COM /问题/ 422879 /最佳Twitter的PHP库 –

回答

0

帖子这个PHP HTML的头顶:

<?php 
//Replace XXXXX with your twitter username 
$twitterUsername = "XXXXX"; 
//Change number to amount of tweets you would like to display 
$amountToShow = 5; 
$twitterRssFeedUrl = 'https://api.twitter.com/1/statuses/user_timeline.rss?screen_name='.$twitterUsername.'&count='.$amountToShow; 

$twitterPosts = false; 
$xml = @simplexml_load_file($twitterRssFeedUrl); 
if(is_object($xml)){ 
    foreach($xml->channel->item as $twit){ 
    if(is_array($twitterPosts) && count($twitterPosts)==$amountToShow){ 
    break; 
} 
$d['title'] = stripslashes(htmlentities($twit->title,ENT_QUOTES,'UTF-8')); 
$description = stripslashes(htmlentities($twit->description,ENT_QUOTES,'UTF-8')); 
if(strtolower(substr($description,0,strlen($twitterUsername))) == strtolower($twitterUsername)){ 
    $description = substr($description,strlen($twitterUsername)+1); 
} 
$d['description'] = $description; 
$d['pubdate'] = strtotime($twit->pubDate); 
$d['guid'] = stripslashes(htmlentities($twit->guid,ENT_QUOTES,'UTF-8')); 
$d['link'] = stripslashes(htmlentities($twit->link,ENT_QUOTES,'UTF-8')); 
$twitterPosts[]=$d; 
} 
}else{ 

die('Can`t fetch the feed you requested'); 
} 
  
?> 

帖子这在HTML的身体:

<ul> 
<?php 
if(is_array($twitterPosts)){ 
echo ''; 
foreach($twitterPosts as $post){ 
$data = $post['description']; 

echo '<li>'.$data.'. '; 
echo '<a href="'.$post['link'].'" class="timestamp">Posted '.date('F j, Y, g:i a',$post['pubdate']).'</a></li>'; 
} 
echo ''; 
}else{ 
    echo 'No Twitter posts have been made';//Error message 
} 

?> 
</ul> 
0

一对夫妇更多的功能,你可以添加的相对时间(即“昨天”“2周前”等),并将网址和用户名转换为链接。

插入下面的代码到你的PHP文件的顶部:

<?php 
    ###################################### 
    ## DISPLAY TWITTER FEED 
    ###################################### 

    //Replace XXXXX with your twitter username 
    $twitterUsername = "XXXXX"; 
    //Change number to amount of tweets you would like to display 
    $amountToShow = 5; 

    $twitterRssFeedUrl = 'https://api.twitter.com/1/statuses/user_timeline.rss?screen_name='.$twitterUsername.'&count='.$amountToShow; 

    $twitterPosts = false; 
    $xml = @simplexml_load_file($twitterRssFeedUrl); 
    if(is_object($xml)){ 
     foreach($xml->channel->item as $twit){ 
     if(is_array($twitterPosts) && count($twitterPosts)==$amountToShow){ 
     break; 
    } 
    $d['title'] = stripslashes(htmlentities($twit->title,ENT_QUOTES,'UTF-8')); 
    $description = stripslashes(htmlentities($twit->description,ENT_QUOTES,'UTF-8')); 
    if(strtolower(substr($description,0,strlen($twitterUsername))) == strtolower($twitterUsername)){ 
     $description = substr($description,strlen($twitterUsername)+1); 
    } 
    $d['description'] = $description; 
    $d['pubdate'] = strtotime($twit->pubDate); 
    $d['guid'] = stripslashes(htmlentities($twit->guid,ENT_QUOTES,'UTF-8')); 
    $d['link'] = stripslashes(htmlentities($twit->link,ENT_QUOTES,'UTF-8')); 
    $twitterPosts[]=$d; 
    } 
    }else{ 

    die('Can`t fetch the feed you requested'); 
    } 
      


    ###################################### 
    ## REPLACE URLS AND USERS WITH LINKS 
    ###################################### 

function hyperlinks($text) { 
    // Props to Allen Shaw & webmancers.com 
    // match protocol://address/path/file.extension?some=variable&another=asf% 
    //$text = preg_replace("/\b([a-zA-Z]+:\/\/[a-z][a-z0-9\_\.\-]*[a-z]{2,6}[a-zA-Z0-9\/\*\-\?\&\%]*)\b/i","<a href=\"$1\" class=\"twitter-link\">$1</a>", $text); 
    $text = preg_replace('/\b([a-zA-Z]+:\/\/[\w_.\-]+\.[a-zA-Z]{2,6}[\/\w\-~.?=&%#+$*!]*)\b/i',"<a href=\"$1\" class=\"twitter-link\">$1</a>", $text); 
    // match www.something.domain/path/file.extension?some=variable&another=asf% 
    //$text = preg_replace("/\b(www\.[a-z][a-z0-9\_\.\-]*[a-z]{2,6}[a-zA-Z0-9\/\*\-\?\&\%]*)\b/i","<a href=\"http://$1\" class=\"twitter-link\">$1</a>", $text); 
    $text = preg_replace('/\b(?<!:\/\/)(www\.[\w_.\-]+\.[a-zA-Z]{2,6}[\/\w\-~.?=&%#+$*!]*)\b/i',"<a href=\"http://$1\" class=\"twitter-link\">$1</a>", $text);  

    // match [email protected] 
    $text = preg_replace("/\b([a-zA-Z][a-zA-Z0-9\_\.\-]*[a-zA-Z]*\@[a-zA-Z][a-zA-Z0-9\_\.\-]*[a-zA-Z]{2,6})\b/i","<a href=\"mailto://$1\" class=\"twitter-link\">$1</a>", $text); 
     //mach #trendingtopics. Props to Michael Voigt 
    $text = preg_replace('/([\.|\,|\:|\¡|\¿|\>|\{|\(]?)#{1}(\w*)([\.|\,|\:|\!|\?|\>|\}|\)]?)\s/i', "$1<a href=\"http://twitter.com/#search?q=$2\" class=\"twitter-link\">#$2</a>$3 ", $text); 
    return $text; 
} 

function twitter_users($text) { 
     $text = preg_replace('/([\.|\,|\:|\¡|\¿|\>|\{|\(]?)@{1}(\w*)([\.|\,|\:|\!|\?|\>|\}|\)]?)\s/i', "$1<a href=\"http://twitter.com/$2\" class=\"twitter-user\">@$2</a>$3 ", $text); 
     return $text; 
}  






    ###################################### 
    ## DISPLAY RELATIVE DATES 
    ###################################### 

//props to Osman Üngür for this (http://goo.gl/FrEMp) 
function time2str($ts) 
{ 
    if(!ctype_digit($ts)) 
     $ts = strtotime($ts); 

    $diff = time() - $ts; 
    if($diff == 0) 
     return 'now'; 
    elseif($diff > 0) 
    { 
     $day_diff = floor($diff/86400); 
     if($day_diff == 0) 
     { 
      if($diff < 60) return 'just now'; 
      if($diff < 120) return '1 minute ago'; 
      if($diff < 3600) return floor($diff/60) . ' minutes ago'; 
      if($diff < 7200) return '1 hour ago'; 
      if($diff < 86400) return floor($diff/3600) . ' hours ago'; 
     } 
     if($day_diff == 1) return 'Yesterday'; 
     if($day_diff < 7) return $day_diff . ' days ago'; 
     if($day_diff == 7) return '1 week ago'; 
     if($day_diff < 31) return ceil($day_diff/7) . ' weeks ago'; 
     if($day_diff < 60) return 'last month'; 
     return date('F Y', $ts); 
    } 
    else 
    { 
     $diff = abs($diff); 
     $day_diff = floor($diff/86400); 
     if($day_diff == 0) 
     { 
      if($diff < 120) return 'in a minute'; 
      if($diff < 3600) return 'in ' . floor($diff/60) . ' minutes'; 
      if($diff < 7200) return 'in an hour'; 
      if($diff < 86400) return 'in ' . floor($diff/3600) . ' hours'; 
     } 
     if($day_diff == 1) return 'Tomorrow'; 
     if($day_diff < 4) return date('l', $ts); 
     if($day_diff < 7 + (7 - date('w'))) return 'next week'; 
     if(ceil($day_diff/7) < 4) return 'in ' . ceil($day_diff/7) . ' weeks'; 
     if(date('n', $ts) == date('n') + 1) return 'next month'; 
     return date('F Y', $ts); 
    } 
} 

    ?> 

然后张贴下面的代码在HTML的身体:

<ul> 
<?php 
if(is_array($twitterPosts)){ 
echo ''; 
foreach($twitterPosts as $post){ 
$data = hyperlinks($post['description']); 
$data = twitter_users($data); 

echo '<li>'.$data.'. '; 
echo '<a href="'.$post['link'].'" class="timestamp">Posted '.time2str(date($post['pubdate'])).'</a></li>'; 
} 
echo ''; 
}else{ 
    echo 'No Twitter posts have been made';//Error message 
} 

?> 
</ul>