如何从html表单读取RSS提要

问题描述 投票:-2回答:2

我正在尝试构建HTML / PHP应用程序,在该应用程序中,您将RSS URL输入到表单字段中,然后由于进入rssfeed.php页面而在浏览器中检索RSS feed。目前,我收到一个错误file_get_contents() expects parameter 1 to be a valid path, array given in C:\xampp\htdocs\week14\rssfeed.php on line 2

这是我的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>

这里是rssfeed.php代码:

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

?>

甚至都不知道要搜索什么,我是从头开始构建代码的新手。

所以这是显示espn的供稿的内容,但我需要对其进行集成,以便在将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>";
php html forms rss rss-reader
2个回答
1
投票

给出一个想法,您可以将一些RSSFeed的URL发布到脚本中,然后对其进行处理,也许这可能有所帮助。

[通常要使用DOMDocumentSimpleXML处理RSSFeed的内容-此处的代码只是将远程XML文件直接直接加载到DOMDocument中,并实例化XPath对象以进一步查询以查找节点。还有许多其他方法可以做到这一点-但很快就以示例形式编写了它。

<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>

输出片段:

<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>

一个更完整的示例,它显示了多个可能的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>

0
投票

授予此权限仅读取http链接,而不读取https:

Index.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:

<?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>";
};
        ?>
© www.soinside.com 2019 - 2024. All rights reserved.