【问题标题】:How to read rss feed from html form如何从 html 表单中读取 rss 提要
【发布时间】:2019-12-10 15:39:58
【问题描述】:

我正在尝试构建一个 HTML/PHP 应用程序,您可以在其中将 RSS URL 输入到表单字段中,然后通过访问 rssfeed.php 页面在浏览器中检索 RSS 提要。目前,我收到一个错误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 放置到此代码 sn-p 中的 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>";

【问题讨论】:

  • type="content" html5doctor.com/html5-forms-input-types - 可能是问题的根源。
  • file_get_contents($_POST); - 您需要指定 $_POST 中的哪个字段用作文件名。
  • 类型应该是 URL,因为我传递了一个完整的 URL,但是我希望它 POST 或嵌入自身,以便它像我转到实际的 RSS Url 时一样显示,但我只能这样做一个实时服务器,所以这可能是问题的一半,因为本地主机不在互联网上。
  • 我需要告诉php文件它正在使用什么文件?

标签: php html forms rss rss-reader


【解决方案1】:

想知道如何将一些RSSFeed 的url 发布到脚本然后处理它,这可能会有所帮助。

要处理 RSSFeed 的内容,通常会使用 DOMDocument 或 SimpleXML - 这里的代码只是将远程 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>

来自输出的 sn-p:

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

【讨论】:

  • 这很有帮助,因为它为我提供了一个开始的地方,我认为 XML 将是必需的,但不确定谢谢。如果我通过表格找到如何做到这一点,我会将其发布回问题中。
  • Erm,已经完成了"through a form" [我只是为了这个演示的目的将一个值硬编码到表单字段中] - 在这种情况下,表单将 URL 发布到同一页面,但可能是你的rssfeed.php 脚本。大概您的rssfeed.php 脚本会处理和显示?如果是,您可以使用上面的 PHP 代码作为 rssfeed.php 脚本的基础...
  • 所以我确实在站点中将它们分开了,但是这个只显示文章标题的标题,你会用什么来显示整个 html 提要?我正在寻找 W3 学校并玩它,但到目前为止还没有。它位于eleanor.codes/week14
【解决方案2】:

当然这只会读取 http 链接而不是 https:

索引.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>";
};
        ?>

【讨论】:

  • 要将file_get_contents 与启用SSL 的链接(不是全部)一起使用,您需要对该函数使用context 参数并将其配置为ssl ~ 这可能会有所帮助。 stackoverflow.com/questions/26148701/…
猜你喜欢
  • 2012-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多