PHP检索最新的推文

时间:2013-02-15 07:04:16

标签: twitter php

我目前正在使用以下代码从Twitter Feed中检索最新的推文:

<?php
    $username='myUsername';
    $format = 'json';
    $tweet = json_decode(file_get_contents("http://api.twitter.com/1/statuses/user_timeline/{$username}.{$format}"));
    $latestTweet = htmlentities($tweet[0]->text, ENT_QUOTES);
    $latestTweet = preg_replace('/http:\/\/([a-z0-9_\.\-\+\&\!\#\~\/\,]+)/i', '<a href="http://$1" target="_blank">http://$1</a>', $latestTweet);
    $latestTweet = preg_replace('/@([a-z0-9_]+)/i', '<a href="http://twitter.com/$1" target="_blank">@$1</a>', $latestTweet);
    echo '<p class="inset white">'.$latestTweet.'</p>';
?>

在MAMP上,这非常有效,但是在我的实际网站上,我什么都没得到,甚至没有服务器端错误消息。我在一篇类似的帖子中读到,这可能与allow_url_fopen = On未在php.ini中设置有关,但设置似乎没有任何区别。

尽管Twitter可能已经改变了他们使用API​​的政策(我认为他们没有),但我对于可能出现的问题感到很遗憾。

1 个答案:

答案 0 :(得分:0)

之前我使用相同的方法,但不幸的是,它使用Twitter API v1和v1不再可用(有关详细信息,请查看https://dev.twitter.com/blog/api-v1-retirement-final-dates)。您现在需要拥有OAuth使用者和用户访问权限。 GitHub themattharris'tmhOAuth库可以方便地显示Twitter提要。

您可以在stackoverflow上查看我在此处发布的问题,作为您问题的参考:

Display latest Twitter Feed "date created" using tmhOAuth and PHP

它已经有代码如何访问需要在Twitter中检索的公共信息。

相关问题