Twitter提要不起作用

问题描述

| 嗨,我使用以下PHP代码来解析Twitter提要,并在网站的页脚中显示最新的两条推文。
<ul id=\"twitter_update_list\" style=\"word-wrap:break-word;\">
<?PHP
    $doc = new DOMDocument();
    $doc->load(\'http://twitter.com/statuses/user_timeline/fixedgearfrenzy.RSS\');
    $arrFeeds = array();

    $count = 0;

    foreach ($doc->getElementsByTagName(\'item\') as $node) 
    {           
        if($count < 2)
            echo(\'<li><span style=\"word-wrap:break-word;\">\'.substr($node->getElementsByTagName(\'description\')->item(0)->nodeValue,17).\' </span><a href=\"\'.$node->getElementsByTagName(\'link\')->item(0)->nodeValue.\'\">\'.substr($node->getElementsByTagName(\'pubDate\')->item(0)->nodeValue,16).\'</a></li>\');

        $count = $count + 1;
    }
?></ul>
由于某些原因,它似乎并不总是有效,并且在大多数情况下,显示以下错误
Warning: DOMDocument::load(http://twitter.com/statuses/user_timeline/fixedgearfrenzy.RSS) [domdocument.load]: Failed to open stream: HTTP request Failed! HTTP/1.1 400 Bad Request in /home/fixedge1/public_html/catalog/view/theme/CartMania-Clean/template/common/footer.tpl on line 336Warning: DOMDocument::load() [domdocument.load]: I/O warning : Failed to load external entity \"http://twitter.com/statuses/user_timeline/fixedgearfrenzy.RSS\" in /home/fixedge1/public_html/catalog/view/theme/CartMania-Clean/template/common/footer.tpl on line 336
我无法弄清楚为什么有时它会起作用而有时却不起作用,有什么想法吗?? 该网站是http://www.fixedgearfrenzy.co.uk,它是右下角的twitter提要     

解决方法

        IIRC,Twitter对加载提要的次数设置了每小时限制。如果您的结果是断断续续的,这可能就是原因。尝试在本地缓存提要结果,以避免一遍又一遍地从Twatter重新加载相同的数据。     ,        使用
DOMDocument
加载远程资源时,应使用有效的
user-agent
<?php

// Set a valid user-agent
$opts = array(
    \'http\' => array(
        \'user_agent\' => \'PHP libxml agent\',)
);

$context = stream_context_create($opts);
libxml_set_streams_context($context);

$doc = new DOMDocument();
$doc->load(\'http://twitter.com/statuses/user_timeline/fixedgearfrenzy.rss\');
$arrFeeds = array();

// rest of your code...
    

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...