直到几个星期前,下面的代码非常适合抓住我的最后三条推文并将其显示在我的网站上。现在它不起作用了。我已经浏览了Twitter的留言板,看看有什么改变无济于事。
有谁知道如何使用php在网站上有效地显示您的最新推文?
我的原始代码在这里。就像我说的那样,这种情况一直持续到几周前:
$twitterUsername = "myUsername";
$amountToShow = 3;
$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中如此出现:
<dl class="twitter">
<dt>Twitter Feed</dt>
<?php
if(is_array($twitterPosts)){
echo '';
foreach($twitterPosts as $post){
$data = hyperlinks($post['description']);
$data = twitter_users($data);
echo '<dd>'.$data.'. ';
echo '<a href="'.$post['link'].'" class="timestamp">Posted '.time2str(date($post['pubdate'])).'</a></dd>';
}
echo '';
}else{
echo 'No Twitter posts have been made';//Error message
}
?>
<dd>
答案 0 :(得分:2)
几周前,您正在使用的Twitter API 1.0已关闭。
在此处阅读API 1.1:https://dev.twitter.com/docs/api
有许多用于处理新API的PHP库,包括mine。
答案 1 :(得分:0)
Twitter REST API v1不再有效。请迁移到API v1.1。 https://dev.twitter.com/docs/api/1.1/overview