简体   繁体   English

带选择(选项)框的RSS php阅读器

[英]RSS php reader with select (option) box

I create this PHP file. 我创建了这个PHP文件。 But this read only one link. 但这仅读取一个链接。 How can I add the other two? 如何添加其他两个? The selection box read on page only one link ... http://www.kurir.rs/rss/vesti/ " 选择框仅在页面上读取一个链接... http://www.kurir.rs/rss/vesti/
http://www.blic.rs/rss/IT http://www.blic.rs/rss/IT

<form action="index.php" method="POST">
    <select name="rss">
        <option value="http://www.kurir.rs/rss/vesti/">Kurir</option>
        <option value="http://www.blic.rs/rss/IT">Blic</option>
        <option value="http://www.b92.net/info/rss/tehnopolis.xml">B92</option>
    </select>
    <input type="submit" value="Select" />
</form>

<?php
$rss = new DOMDocument();
$rss->load('http://www.b92.net/info/rss/tehnopolis.xml');

$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>';
}

?> ?>

The simple answer, you need to use what is posted from your form to load the page. 简单的答案是,您需要使用表单中发布的内容来加载页面。 So something like this: 所以像这样:

$rss_url = isset($_REQUEST['rss']) ? $_REQUEST['rss'] : 'http://www.b92.net/info/rss/tehnopolis.xml';
$rss = new DOMDocument();
$rss->load( $rss_url );

I even threw some validation in there to check if $_REQUEST['rss'] is set. 我什至在其中进行了一些验证,以检查是否设置了$_REQUEST['rss']

Is this the best way to do this? 这是最好的方法吗? no. 没有。 You would need to further validate your input so people can post something unexpected. 您需要进一步验证您的输入,以便人们可以发布意外内容。 Also using POST, may be unnecessary for this. 同样也可以使用POST。 GET might work just fine. GET可能效果很好。 But for this exercise, it will work. 但是对于此练习,它将起作用。

Also, if you wanted the option box to show the selected url: 另外,如果您希望选项框显示所选的URL:

<form action="index.php" method="POST">
    <select name="rss">


<?php
$selection = array (
    'Kurir' => 'http://www.kurir.rs/rss/vesti/', 
    'Blic' => 'http://www.blic.rs/rss/IT', 
    'B92' => 'http://www.b92.net/info/rss/tehnopolis.xml' );

foreach ($selection as $title => $url) {
    if(! empty($_REQUEST) and isset($_REQUEST['rss']) and $_REQUEST['rss'] == $url ){
        $selected = 'selected';       
    } else {
        $selected = '';
    }
    print'<option value="'.$url.'" '.$selected.'>'.$title.'</option>';
    print "\n";
}


?>

    </select>
    <input type="submit" value="Select" />
</form>

<?php
$rss_url = isset($_REQUEST['rss']) ? $_REQUEST['rss'] : 'http://www.b92.net/info/rss/tehnopolis.xml';
print $rss_url;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM