用php获取rss提要并更改rss字符串中的值



好的,我有一个网站,在那里我使用这个代码来获取搜索到的每只股票的股票报价。<?php echo $_GET['quote'];?>我想做的是使用以下代码显示RSS新闻数据:

<?php
$rss = new DOMDocument();
$rss->load('http://feeds.finance.yahoo.com/rss/2.0/headline?s=GOOG&region=US&lang=en-USsto');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = 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($feed, $item);
}
$limit = 5;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
echo '<small><em>Posted on '.$date.'</em></small></p>';
echo '<p>'.$description.'</p>';
}
?>

你看到"谷歌"部分了吗?这就是我试图通过引用捕获代码<?php echo $_GET['quote'];?>动态更改的内容,它会抛出错误!还有别的办法吗?

如果您的GET值包含合法的股票代码,那么这将起作用。

$rss->load('http://feeds.finance.yahoo.com/rss/2.0/headline?s='.$_GET['quote'].'&region=US&lang=en-USsto');

您已经处于php上下文中,因此不是连接字符串的方法

然而,这不是一种稳健的处理方式,因为没有检查$_GET[‘quote’]是否设置或有任何值,如果没有设置,您需要决定要得到什么

更新

注意,问题中给出的原始URL是无效的

http://feeds.finance.yahoo.com/rss/2.0/headline?s=GOOG&region=US&lang=en-USsto

不起作用,但

http://feeds.finance.yahoo.com/rss/2.0/headline?s=GOOG&region=US&lang=en-US

因此,请将您的代码更新为

$rss->load('http://feeds.finance.yahoo.com/rss/2.0/headline?s='.$_GET['quote'].'&region=US&lang=en-US');

最新更新