我尝试了大约15种不同的代码变体,但都没有成功。基本上,我只想在Wordpress网站上发布我的最新推文。下面的代码来自Smashing Magazine,很明显,在我的代码中,我已经将$username更改为我的Twitter用户名。到目前为止,一切都不起作用,我不知道它是什么。它正在连接到Twitter,因为当代码在那里时,加载页面需要更长的时间。我也试过一个,它显示了我有多少粉丝,并显示了我的用户名,但没有显示我的推特。这是我的网站,如果它有帮助的话。。。
http://newmedia.leeds.ac.uk/ug10/cs10cwh/wordpress/
为了加快加载时间,我暂时删除了代码。任何帮助都将不胜感激。
<?php
// Your twitter username.
$username = "TwitterUsername";
// 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 = "<h2>My last Tweet</h2>";
// 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("<", "<", $tweet);
$tweet = str_replace(">", ">", $tweet);
return $tweet;
}
$twitterFeed = file_get_contents($feed);
echo stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix);
?>
代码没有问题。
可能性1:您提供了错误/无效的$username
。
可能性2:file_get_contents
没有检索内容。(由于PHP设置、网络问题或其他原因)