简体   繁体   English

如何从html表单读取RSS提要

[英]How to read rss feed from html form

I am trying to build an HTML/PHP application where you type in an RSS URL into a form field and then retrieve the RSS feed in the browser as a result of going to the rssfeed.php page.我正在尝试构建一个 HTML/PHP 应用程序,您可以在其中将 RSS URL 输入到表单字段中,然后作为转到 rssfeed.php 页面的结果在浏览器中检索 RSS 提要。 Currently, I am getting an error file_get_contents() expects parameter 1 to be a valid path, array given in C:\\xampp\\htdocs\\week14\\rssfeed.php on line 2目前,我收到一个错误file_get_contents() expects parameter 1 to be a valid path, array given in C:\\xampp\\htdocs\\week14\\rssfeed.php on line 2

Here is my index.html code:这是我的 index.html 代码:

<html>

<body>

<form action="rssfeed.php" method="post">
Please enter the RSS feed URL: <input type="content" name="name"><br>
<input type="submit">
</form>

</body>
</html>

Here is the rssfeed.php code:这是 rssfeed.php 代码:

<?php
$content = file_get_contents($_POST); 

?>

Not even sure what to google anymore, I am new to building code from scratch.甚至不确定要谷歌什么,我是从头开始构建代码的新手。

So this is what displays a feed from espn but I need to integrate this so that when I type the URL into the form it places that one into the place where the url in this code snippet currently is:所以这就是显示来自 espn 的提要的内容,但我需要将其集成,以便当我在表单中键入 URL 时,它将该 URL 放置到此代码片段中当前的 URL 所在的位置:

$feed_url = "http://rssfeeds.usatoday.com/UsatodaycomGolf-TopStories";
$content = file_get_contents($feed_url);
$x = new SimpleXmlElement($content);

echo '<ul>';
foreach($x->channel->item as $entry) {
   echo '<li>';
   echo '<a href="'.$entry->link.'" title="'.$entry->title.'" target="_blank">' . $entry->title . '</a>'; // output link & title
   echo $entry->description; // return post content
   echo '</li>';
}
echo "</ul>";

To give an idea how you can POST a url for some RSSFeed to a script and then process it perhaps this might help.要了解如何将某些RSSFeed的 url 发布到脚本然后对其进行处理,这可能会有所帮助。

To process the contents of an RSSFeed typically one would use DOMDocument or SimpleXML - the code here simply loads the remote XML file directly into the DOMDocument and instantiates an XPath object to further query to find nodes.要处理 RSSFeed 的内容,通常会使用DOMDocumentSimpleXML - 这里的代码只是将远程 XML 文件直接加载到DOMDocument并实例化一个 XPath 对象以进一步查询以查找节点。 There are many other ways one could do this - but it was quickly written just as example.有许多其他方法可以做到这一点 - 但它很快就被写成了例子。

<html>
    <head>
        <title>RSS</title>
    </head>
    <body>
        <form method='post'>
            <input type='text' name='name' value='https://www.huffingtonpost.co.uk/feeds/index.xml' />
            <input type='submit'>
        </form>
        <?php
            if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_POST['name'] ) ){

                $dom=new DOMDocument;
                $dom->load( $_POST['name'] );

                $xp=new DOMXPath( $dom );
                $col=$xp->query( '//channel/item' );

                if( $col->length > 0 ){
                    foreach( $col as $node ){
                        printf( 
                            '<h3>%s</h3>', 
                            $xp->query('title',$node)->item(0)->textContent
                        );
                    }
                }
            }
        ?>
    </body>
</html>

a snippet from the output:输出中的一个片段:

<h3>Where To Travel In 2020: The Top 10 Emerging Destinations</h3>
<h3>Gavin And Stacey Christmas Special: First Reviews Of Reunion Episode Are In</h3> 
<h3>Jeremy Corbyn Speaks To The Common People | The People's Election</h3>

A more complete example showing multiple possible RSS feeds and more fields from each article:一个更完整的示例,显示了每篇文章中多个可能的 RSS 提要和更多字段:

<html>
    <head>
        <title>RSS</title>
    </head>
    <body>
        <form method='post'>
            <select name='name'>
                <optgroup label='World news'>
                    <option>http://rssfeeds.usatoday.com/UsatodaycomGolf-TopStories
                    <option>http://feeds.reuters.com/Reuters/worldNews
                    <option>http://rss.cnn.com/rss/edition_world.rss
                </optgroup>
                <optgroup label='UK news'>
                    <option>http://feeds.bbci.co.uk/news/rss.xml
                    <option>http://feeds.skynews.com/feeds/rss/uk.xml
                    <option>http://feeds.reuters.com/reuters/UKdomesticNews
                </optgroup>
                <optgroup label='US news'>
                    <option>http://rssfeeds.usatoday.com/usatoday-newstopstories
                    <option>http://feeds.reuters.com/Reuters/domesticNews
                    <option>http://feeds.skynews.com/feeds/rss/us.xml
                </optgroup>
                <optgroup label='Miscellaneous'>
                    <option>https://www.wired.com/feed
                    <option>http://rss.slashdot.org/Slashdot/slashdot
                    <option>https://www.huffingtonpost.co.uk/feeds/index.xml
                </optgroup>
            </select>
            <input type='submit'>
        </form>
        <?php
            if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_POST['name'] ) ){

                $url=$_POST['name'];
                $max=150;
                $ellipsis=str_repeat('.',5);




                $dom=new DOMDocument;
                $dom->load( $url );

                $xp=new DOMXPath( $dom );
                $col=$xp->query( '//channel/item' );

                if( $col->length > 0 ){


                    echo '<ul>';

                    foreach( $col as $node ){
                        try{
                            $description=$xp->evaluate('string(description)',$node);
                            if( strlen( $description ) > $max )$description=substr( $description, 0, $max ) . $ellipsis;

                            $category=$xp->evaluate( 'string(category)', $node );

                            printf( 
                                '<li>
                                    <a href="%s" target="_blank">%s</a>
                                    <div>Category: %s</div>
                                    <p>%s</p>
                                </li>',
                                $xp->evaluate( 'string(link)', $node ),
                                $xp->evaluate( 'string(title)',$node ),
                                $category,
                                $description
                            );
                        }catch( Exception $e ){
                            continue;
                        }
                    }

                    echo '</ul>';

                } else {
                    echo 'nothing found for given XPath query';
                }
            }
        ?>
    </body>
</html>

Granted this only reads http links not https:当然,这只会读取 http 链接而不是 https:

Index.html:索引.html:

<!DOCTYPE html>

<html>

    <head>
            <link rel="stylesheet" href="styles.css" type="text/css">
        <title>RSS Feed Search</title>
    </head>
    <body>
        <h2>RSS Search</h2>
        <form form action="rss.php" method="post">
            <input type='text' name='name' value='' />
            <input type='submit'>
        </form>

    </body>

</html>

rss.php: rss.php:

<?php
if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_POST['name'] ) ){
$feed_url = ( $_POST['name'] );
$content = file_get_contents($feed_url);
$x = new SimpleXmlElement($content);

echo '<ul>';
foreach($x->channel->item as $entry) {
   echo '<li>';
   echo '<a href="'.$entry->link.'" title="'.$entry->title.'" target="_blank">' . $entry->title . '</a>'; // output link & title
   echo $entry->description; // return post content
   echo '</li>';
}
echo "</ul>";
};
        ?>

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

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