Twitter RSS 提要,[domdocument.load]:无法打开流:
我正在使用以下命令:
<?php
$doc = new DOMDocument();
$doc->load('http://twitter.com/statuses/user_timeline/XXXXXX.rss');
$arrFeeds = array();
foreach ($doc->getElementsByTagName('item') as $node) {
$itemRSS = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
);
array_push($arrFeeds, $itemRSS);
}
for($i=0;$i<=3;$i++) {
$tweet=substr($arrFeeds[$i]['title'],17);
$tweetDate=strtotime($arrFeeds[$i]['date']);
$newDate=date('G:ia l F Y ',$tweetDate);
if($i==0) { $b='style="border:none;"'; }
$tweetsBox.='<div class="tweetbox" ' . $b . '>
<div class="tweet"><p>' . $tweet . '</p>
<div class="tweetdate"><a href="http://twitter.com/XXXXXX">@' . $newDate .'</a></div>
</div>
</div>';
}
return $tweetsBox;
?>
从给定时间线返回 4 条最新的推文(XXXXX 是相关提要)
它似乎工作正常,但我最近偶尔收到以下错误:
PHP 错误调试 错误: DOMDocument::load(http://twitter.com/statuses/user_timeline/XXXXXX.rss ) [domdocument.load]: 无法打开流: HTTP 请求失败! HTTP/1.1 502 Bad Gateway
我读到上面的代码依赖于 Twitter 的可用,并且我知道它有时会变得相当繁忙。是否有更好的方法来接收推特,或者是否有任何类型的错误捕获我可以做,只是为了显示“推文当前不可用...”消息而不是导致错误。我使用的是 ModX CMS,因此任何解析错误都会杀死该站点,而不仅仅是输出警告。
谢谢。
i'm using the following:
<?php
$doc = new DOMDocument();
$doc->load('http://twitter.com/statuses/user_timeline/XXXXXX.rss');
$arrFeeds = array();
foreach ($doc->getElementsByTagName('item') as $node) {
$itemRSS = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
);
array_push($arrFeeds, $itemRSS);
}
for($i=0;$i<=3;$i++) {
$tweet=substr($arrFeeds[$i]['title'],17);
$tweetDate=strtotime($arrFeeds[$i]['date']);
$newDate=date('G:ia l F Y ',$tweetDate);
if($i==0) { $b='style="border:none;"'; }
$tweetsBox.='<div class="tweetbox" ' . $b . '>
<div class="tweet"><p>' . $tweet . '</p>
<div class="tweetdate"><a href="http://twitter.com/XXXXXX">@' . $newDate .'</a></div>
</div>
</div>';
}
return $tweetsBox;
?>
to return the 4 most recent tweets from a given timeline (XXXXX is the relevant feed)
It seems to work fine but i've recently been getting the following error sporadically:
PHP error debug
Error: DOMDocument::load(http://twitter.com/statuses/user_timeline/XXXXXX.rss) [domdocument.load]: failed to open stream: HTTP request failed! HTTP/1.1 502 Bad Gateway
I've read that the above code is dependant on Twitter beign available and I know it gets rather busy sometimes. Is there either a better way of receiving twits, or is there any kind of error trapping i could do to just to display "tweets are currently unavailable..." ind of message rather than causing an error. I'm usnig ModX CMS so any parse error kills the site rather than just ouputs a warning.
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我知道这已经很旧了,但我只是在寻找相同的解决方案,以获取几乎相同的脚本来抓取 Twitter 时间线。我最终做到了这一点,尽管我还没有能够彻底测试它。
我将 twitter url 定义为变量 ($feedURL),我也在 $doc_load 中使用了该变量。然后,我将除 $feedURL 之外的所有内容都包装到这个条件语句中:
因此,它只是检查提要页面的标头,如果其状态为 200(正常),则脚本的其余部分将执行。否则,它将回显您选择的消息。
(参考:
ETA:或者更好的是,保存提要的缓存版本(这也将确保您不会超出 API 负载限制):
然后在您的
$doc->; 中使用 $cache_file load($cache_file)
语句而不是实际的 feed url。(改编自此处:http://snipplr.com/view/8156/twitter-cache/ )。
I know this is old, but I was just searching for the same solution for a nearly identical script for grabbing a twitter timeline. I ended up doing this, though I haven't been able to thoroughly test it.
I defined the twitter url as a variable ($feedURL), which I also used in $doc_load. Then, I wrapped everything except for the $feedURL into this conditional statement:
So, it's just checking the headers of the the feed's page, and if its status is 200 (OK), then the rest of the script will execute. Otherwise, it'll echo a message of your choice.
(reference: http://www.phptalk.com/forum/topic/3940-how-to-check-if-an-external-url-is-valid-andor-get-file-size/ )
ETA: Or even better, save a cached version of the feed (which will also ensure you don't go over your API limit of loads):
Then use $cache_file in your
$doc->load($cache_file)
statement instead of the actual feed url.(Adapted from here: http://snipplr.com/view/8156/twitter-cache/).