dongluanjie8678 2012-05-07 10:07
浏览 44
已采纳

如何在Twitter RSS上发布可点击的推文链接

I use SimpleXML to get my tweets via RSS. I want to make links clickable on the tweets. But i could not figure how. I tried somethings that did not work. Your helps needed.

Here is my code;

<? 
$twitterRssFeedUrl =  "https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=puzzletravel";
$twitterUsername = "puzzletravel";
$amountToShow = 5;

$twitterPosts = false;
$xml = @simplexml_load_file($twitterRssFeedUrl);
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
$text = $xml;
if(is_object($xml)){
    //Rest of our code will be here
}else{
    die('cannot connect to twitter feed');
}

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;
}

if(is_array($twitterPosts)){
    echo '<ul>';
    foreach($twitterPosts as $post){
        echo '<li><p>'.$post['description'].'</p><p class="date">'.date(' j F',$post['pubdate']).'</p></li>';
    }
    echo '</ul>';
}else{
    echo '<p>No Twitter posts have been made</p>';
}

?>
  • 写回答

2条回答 默认 最新

  • douxia6163 2012-05-10 08:56
    关注

    I found a solution for my problem. Here is the solution for those whom might need. This solution converts text which contains http,https to clickable url.

    <? 
    $twitterRssFeedUrl =  "https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=puzzletravel";
    $twitterUsername = "puzzletravel";
    $amountToShow = 7;
    
    $twitterPosts = false;
    $xml = @simplexml_load_file($twitterRssFeedUrl);
    $reg_exUrl = "/(http|https)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
    
    if(is_object($xml)){
        //Rest of our code will be here
    }else{
        die('Tweetler Alinamiyor');
    }
    
    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;
    }
    
    if(is_array($twitterPosts)){
        echo '<ul>';
        foreach($twitterPosts as $post){
    
            if(preg_match($reg_exUrl, $post['description'], $url)) {
                $post['description']=preg_replace($reg_exUrl, "<a href={$url[0]}>{$url[0]}</a> ", $post['description']);
            }
    
            echo '<ul id="twitter_update_list">
                  <li>
        <div id="tweet">'.$post['description'].'<a href="'.$post['link'].'">'.date(' j F',$post['pubdate']).'<br></a></li>';
        }
        echo '</ul>';
    }else{
        echo '<p>Güncel Tweetler Oluşturulamadı Sayfayı Yenilemeyi Deneyin.</p></div>';
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装