0

我在网站上设置了“最新推文”功能。客户现在要求我隐藏@提及以阻止它们显示为“最新推文”。我看过 Twitter API,但老实说,我对此知之甚少,因此无法真正了解如何做到这一点。或者即使有可能。

我用来调用“最新推文”的这段代码是

<?php
            // Your twitter username.
            $username = "****";

            // Prefix - some text you want displayed before your latest tweet.
            // (HTML is OK, but be sure to escape quotes with backslashes: for example href=\"link.html\")
            $prefix = "";

            // Suffix - some text you want display after your latest tweet. (Same rules as the prefix.)
            $suffix = "";

            $feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=1";

            function parse_feed($feed) {
                $stepOne = explode("<content type=\"html\">", $feed);
                $stepTwo = explode("</content>", $stepOne[1]);
                $tweet = $stepTwo[0];
                $tweet = str_replace("&lt;", "<", $tweet);
                $tweet = str_replace("&gt;", ">", $tweet);
                return $tweet;
            }

            $twitterFeed = file_get_contents($feed);
            echo stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix);
        ?>​

任何帮助将不胜感激。

4

2 回答 2

0

尝试,改变你返回:

if(!preg_match("/@(\w+)/", $tweet))
{
    return $tweet;
}
else
{
    return "";
}
于 2012-04-27T15:51:33.193 回答
0

似乎您想获取用户最新的推文,然后过滤掉 @'s。我会避免使用搜索调用,而是专注于时间线调用

呼叫中的每个项目都附有这些字段

 "in_reply_to_status_id": null,
 "in_reply_to_status_id_str": null,
 "in_reply_to_user_id": null,
 "in_reply_to_user_id_str": null,
 "in_reply_to_screen_name": null,

"in_reply_to_status_id"并且"in_reply_to_status_id_str"可能仍然存在null,它仍然可能是一个@答复。你要找的是"in_reply_to_screen_name". 如果填写了姓名,@则为回复。

拨打电话时,只需忽略其中的项目in_reply_to_screen_name != null

看看开发者控制台,你可以在其中进行测试、混合和匹配。

-- PHP中的示例代码☟</p>

<?php
$json = file_get_contents("http://twitter.com/status/user_timeline/twitterapi.json", true); 
$decode = json_decode($json, true); //PHP Array output from twitter JSON

$valid = array();
$count = count($decode); //counting the number of status
for($i=0;$i<$count;$i++){
    if(!$decode[$i]['in_reply_to_screen_name']){
        array_push($valid, $decode[$i]); 
    }
};

print_r($valid); //tweets that are not @ replies
?>
于 2012-04-27T19:32:14.770 回答