【问题标题】:PHP JSON Twitter Feed ImprovementPHP JSON Twitter 提要改进
【发布时间】:2011-08-24 19:02:46
【问题描述】:

我正在为我的web-site 开发一个 PHP 推特提要。到目前为止,我已经弄清楚如何输出日期,但我想做的是挑选出网址并超链接它们。我还想挑选#hashtags,将它们超链接到搜索推特。我刚开始学习 PHP,所以任何帮助将不胜感激。

<?php
    $count = 5;
    $tweet=json_decode(file_get_contents("http://api.twitter.com/1/statuses/user_timeline/atrueresistance.json?count=".$count."" ));
    for ($i=1; $i <= $count; $i++){   
        echo "<div class='tweet'>".$tweet[($i-1)]->text."                
            <div class='tweet_date'>". date("M \- j",strtotime($tweet[($i-1)]->created_at))."
            </div>
        </div>";   
     }    
?>

【问题讨论】:

    标签: php twitter file-get-contents json


    【解决方案1】:

    由于没有人对此做出回应,我想是时候放出我想出的代码了。

    享受吧!

    <?php
    
    $count = 5;
    $tweet=json_decode(file_get_contents("http://api.twitter.com/1/statuses/user_timeline/atrueresistance.json?count=".$count."" ));
    for ($i=1; $i <= $count; $i++){
        //Assign feed to $feed
        $feed = $tweet[($i-1)]->text;
        //Find location of @ in feed
        $feed = str_pad($feed, 3, ' ', STR_PAD_LEFT);   //pad feed     
        $startat = stripos($feed, '@'); 
        $numat = substr_count($feed, '@');
        $numhash = substr_count($feed, '#'); 
        $numhttp = substr_count($feed, 'http'); 
        $feed = preg_replace("/(http:\/\/)(.*?)\/([\w\.\/\&\=\?\-\,\:\;\#\_\~\%\+]*)/", "<a href=\"\\0\">\\0</a>", $feed);
        $feed = preg_replace("(@([a-zA-Z0-9\_]+))", "<a href=\"http://www.twitter.com/\\1\">\\0</a>", $feed);
        $feed = preg_replace('/(^|\s)#(\w+)/', '\1<a href="http://search.twitter.com/search?q=%23\2">#\2</a>', $feed);
        echo "<div class='tweet'>".$feed.  "<div class='tweet_date'>". date("M \- j",strtotime($tweet[($i-1)]->created_at))."
                </div></div>";      
        }?>
    

    【讨论】:

    • 我建议使用 CURL vs file_get_contents
    猜你喜欢
    • 1970-01-01
    • 2012-06-22
    • 2012-02-28
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多