dpcj32769
2016-12-18 18:54
浏览 57

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

I create this PHP file. 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.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>';
}

?>

图片转代码服务由CSDN问答提供 功能建议

我创建了这个PHP文件。 但这只读了一个链接。 如何添加其他两个? 选择框仅在页面上读取一个链接... http://www.kurir.rs/rss / vesti /
http://www.blic.rs/rss / IT

 &lt; form action =“index.php”method =“POST”&gt; 
&lt; select name =“rss”&gt; \  n&lt; option value =“http://www.kurir.rs/rss/vesti/”&gt; Kurir&lt; / option&gt; 
&lt; option value =“http://www.blic.rs/rss/IT  “&gt; Blic&lt; / option&gt; 
&lt; option value =”http://www.b92.net/info/rss/tehnopolis.xml“&gt; B92&lt; / option&gt; 
&lt; / select&gt; 
  &lt; input type =“submit”value =“选择”/&gt; 
&lt; / form&gt; 
 
&lt;?php 
 $ rss = new DOMDocument(); 
 $ rss-&gt; load('http  ://www.b92.net/info/rss/tehnopolis.xml'); 
 
 $ feed = array(); 
foreach($ rss-&gt; getElementsByTagName('item')as $ node){\  n $ item = array(
'title'=&gt; $ node-&gt; getElementsByTagName('title') - &gt; item(0) - &gt; nodeValue,
'desc'=&gt; $ n  ode-&gt; getElementsByTagName('description') - &gt; item(0) - &gt; nodeValue,
'link'=&gt;  $ node-&gt; getElementsByTagName('link') - &gt; item(0) - &gt; nodeValue,
'date'=&gt;  $ node-&gt; getElementsByTagName('pubDate') - &gt; item(0) - &gt; nodeValue,
); 
 array_push($ feed,$ item); 
} 
 $ limit = 5; 
for  ($ x = 0; $ x&lt; $ limit; $ x ++){
 $ title = str_replace('&amp;','&amp; amp;',$ feed [$ x] ['title']); 
  $ link = $ feed [$ x] ['link']; 
 $ description = $ feed [$ x] ['desc']; 
 $ date = date('l F d,Y',strtotime($  feed [$ x] ['date'])); 
 echo'&lt; p&gt;&lt; strong&gt;&lt; a href =“'。$ link。'”title =“'。$ title。'”&gt;  '。$ title。'&lt; / a&gt;&lt; / strong&gt;&lt; br /&gt;'; 
 echo'&lt; small&gt;&lt; em&gt;张贴于'。$ date。'&lt; / em&gt;&lt;  ; / small&gt;&lt; / p&gt;'; 
 echo'&lt; p&gt;'。$ description。'&lt; / p&gt;'; 
} 
   
 
 <  p>?&gt;  
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dpwjx32578146 2016-12-18 20:28
    已采纳

    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.

    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. GET might work just fine. But for this exercise, it will work.

    Also, if you wanted the option box to show the selected 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 "
    ";
    }
    
    
    ?>
    
        </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;
    
    点赞 打赏 评论

相关推荐 更多相似问题