0

我想知道为什么下面的代码我试图用来拉入最新推文的推特提要,不起作用?

代码:

<?
    $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("&#8211;", "&mdash;", $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)) {

我在这里做错了什么???

4

3 回答 3

2

查看推特 API 更新。

$feed将变量更改为:

$feed = 'http://api.twitter.com/1/statuses/user_timeline.rss?screen_name='.$username.'&count='.$limit;
于 2012-11-10T16:45:54.527 回答
1

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)) .. 在执行循环之前。

于 2012-04-20T13:15:12.287 回答
1

打开php.ini并启用allow_url_fopen. 这不能用ini_set().

或者,使用其他请求 URL 的方法,例如 cURL。

于 2012-04-20T13:17:10.123 回答