[英]Twitter Feed not pulling in with code?
我想知道為什么我嘗試使用以下代碼提取最新tweet的Twitter feed中的代碼?
碼:
<?
$username = "readitforward";
$limit = 5;
$feed = 'http://twitter.com/statuses/user_timeline.rss?screen_name='.$username.'&count='.$limit;
$tweets = file_get_contents($feed);
$tweets = str_replace("&", "&", $tweets);
$tweets = str_replace("<", "<", $tweets);
$tweets = str_replace(">", ">", $tweets);
$tweet = explode("<item>", $tweets);
$tcount = count($tweet) - 1;
for ($i = 1; $i <= $tcount; $i++) {
$endtweet = explode("</item>", $tweet[$i]);
$title = explode("<title>", $endtweet[0]);
$content = explode("</title>", $title[1]);
$content[0] = str_replace("–", "—", $content[0]);
$content[0] = preg_replace("/(http:\/\/|(www\.))(([^\s<]{4,68})[^\s<]*)/", '<a href="http://$2$3" target="_blank">$1$2$4</a>', $content[0]);
$content[0] = str_replace("$username: ", "", $content[0]);
$content[0] = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $content[0]);
$content[0] = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $content[0]);
$mytweets[] = $content[0];
}
while (list(, $v) = each($mytweets)) {
$tweetout .= "<div>$v</div>\n";
}
?>
輸出錯誤:
Warning: file_get_contents() [function.file-get-contents]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in /data/24/1/0/139/1815302/user/1967139/htdocs/RIF/wp-content/themes/crown_readitforward2012/sidebar.php on line 93
Warning: file_get_contents(http://twitter.com/statuses/user_timeline.rss?screen_name=readitforward&count=5) [function.file-get-contents]: failed to open stream: no suitable wrapper could be found in /data/24/1/0/139/1815302/user/1967139/htdocs/RIF/wp-content/themes/crown_readitforward2012/sidebar.php on line 93
Warning: Variable passed to each() is not an array or object in /data/24/1/0/139/1815302/user/1967139/htdocs/RIF/wp-content/themes/crown_readitforward2012/sidebar.php on line 114
第93行: $tweets = file_get_contents($feed);
第114行: while (list(, $v) = each($mytweets)) {
我在這里做錯了什么???
查看Twitter API更新。
將$feed
變量更改為:
$feed = 'http://api.twitter.com/1/statuses/user_timeline.rss?screen_name='.$username.'&count='.$limit;
allow_url_fopen已關閉,因此您必須以另一種方式獲取遠程文件。
http://php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen
您可以改用cURL或其他php擴展名。
http://php.net/manual/en/curl.examples-basic.php
對於第二個錯誤,請在執行循環之前檢查if(is_array($ mytweets))..
打開php.ini
並啟用allow_url_fopen
。 這不能通過ini_set()
。
或者,使用其他方法來請求URL,例如cURL。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.