0

Twitterフィードをプルして表示するPHPスクリプトがサイトにあります。不思議なことに、ほとんどの場合は問題なく動作しているように見えますが、場合によっては(実際にはかなり多く)まったく動作せず、フォローボタンが表示されるだけです。

コードは次のとおりです。明らかUSERNAMEに実際のTwitterアカウントのユーザー名は次のとおりです。

$widget = true;
$twitterid = "@USERNAME";

$doc = new DOMDocument();

# load the RSS document, edit this line to include your username or user id
if($doc->load('http://twitter.com/statuses/user_timeline/USERNAME.rss')) {

    # specify the number of tweets to display, max is 20
    $max_tweets = 4;    

    $i = 1;
    foreach ($doc->getElementsByTagName('item') as $node) {
        # fetch the title from the RSS feed. 
        # Note: 'pubDate' and 'link' are also useful (I use them in the sidebar of this blog)
        $tweet = $node->getElementsByTagName('title')->item(0)->nodeValue;

        # the title of each tweet starts with "username: " which I want to remove
        $tweet = substr($tweet, stripos($tweet, ':') + 1);   

        # OPTIONAL: turn URLs into links
        $tweet = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>', $tweet);

        # OPTIONAL: turn @replies into links
        $tweet = preg_replace("/@([0-9a-zA-Z]+)/", "<a href=\"http://twitter.com/$1\">@$1</a>", $tweet);

        echo "<p> <p>".$tweet."</p></p><hr />\n";

        if ($i++ >= $max_tweets)
            break;
    }
    echo "</ul>\n";
} 

// Here's the Twitter Follow Button Widget
if($widget){
    echo "<a href=\"https://twitter.com/" .$twitterid. "\" class=\"twitter-follow-button\" data-show-count=\"true\">Follow @" .$twitterid. "</a><script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=\"//platform.twitter.com/widgets.js\";fjs.parentNode.insertBefore(js,fjs);}}(document,\"script\",\"twitter-wjs\");</script>";
}
4

3 に答える 3

0

Twitterは、認証されていない呼び出し(OAuthを使用して認証されていないAPIへの呼び出し)にレート制限を適用します。

「認証されていない通話は、1時間あたり150件のリクエストが許可されます。認証されていない通話は、リクエストを行っているサーバーまたはデバイスの公開IPに対して測定されます。」

共有ホスティングを使用している場合、ホスト上で同じIPを使用している他の誰かがTwitter APIにクエリを実行している可能性があるため、レート制限が発生する可能性が高くなります(したがって、そのIPの1時間あたりの制限にカウントされます)。

これらの制限の詳細については、Twitterのレート制限制限Webサイトおよびレート制限FAQWebサイトを参照してください。

于 2012-10-12T16:49:10.637 に答える
0

残念ながら、TwitterはURL https://twitter.com/statuses/user_timeline/USERNAME.rssを削除し、「申し訳ありませんが、そのページは2012年10月12日現在存在していません」と返されます。jsonに相当するものがありますが、これも失敗する可能性があります。 2013年3月。当面はhttps://api.twitter.com/1/statuses/user_timeline.json?screen_name=USERNAME&count=4をお試しください。

HTH

于 2012-10-13T11:58:58.250 に答える
0
<?php

$timeline="http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=arvizard";
$xml= new SimpleXMLElement(file_get_contents($timeline));
$i=0;
print "<ul class=\"tweet_list\">";
foreach($xml ->children() as $tstatus)
    {
        $stat=$tstatus->text;
        $split= preg_split('/\s/',$stat);
        print "<li class=\"tweet\"><p class=\"tweet_text\">";
        foreach ($split as $word)
            {

                if (preg_match('/^@/',$word)) {
                               print " "."<a href=http://www.twitter.com/".substr($word,1).">".$word."</a>";
                               }
                               else if (preg_match('/^http:\/\//',$word)){
                                     print " "."<a href=".$word.">".$word."</a>";
                               }
                               else
                                   {
                                   print " ".$word;
                               }


            }
print "</p>";
print "<span class=\"date\">".substr($tstatus->created_at,0,strlen($tstatus->created_at)-14)."</span>";
print "</li>";
$i++;
if ($i==5)
    {

        break;
    }
    }
print "</ul>";
?>

これはあなたを助けるかもしれません。これをチェックしてください。

于 2013-03-02T07:04:07.610 に答える