简体   繁体   中英

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

Here is my index.html code:

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

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

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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