0

SimpleXMLを使用してRSS経由でツイートを取得しています。ツイートのリンクをクリックできるようにしたい。しかし、私はその方法を理解できませんでした。うまくいかないことを試しました。あなたの助けが必要です。

これが私のコードです。

<? 
$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>';
}

?>
4

2 に答える 2

0

これを変える:

    echo '<li><p>'.$post['description'].'</p><p class="date">'.date(' j F',$post['pubdate']).'</p></li>';

これに:

    echo '<li><p><a href="'.$post['link'].'">'.$post['description'].'</p><p class="date">'.date(' j F',$post['pubdate']).'</a></p></li>';
于 2012-05-07T10:26:51.870 に答える
0

問題の解決策を見つけました。そんな方への解決策をご紹介します。このソリューションは、http、https を含むテキストをクリック可能な 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>';
}

?>
于 2012-05-10T08:56:20.757 に答える