-3

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>";
mildlylost
  • 57
  • 6
  • 1
    `type="content"` < last I knew, there isn't an input of that type. http://html5doctor.com/html5-forms-input-types/ - and is probably the root of the problem. – Funk Forty Niner Dec 10 '19 at 15:42
  • 1
    `file_get_contents($_POST);` - you will need to specify which field in `$_POST` to use as the filename. – Nigel Ren Dec 10 '19 at 15:43
  • the type should be URL since I am passing a URL thorough but then I want it to POST or embed itself so that it displays like when I go to the actual RSS Url but I can only do this with a live server so that could be half the problem because local host is not on the internet. – mildlylost Dec 10 '19 at 15:48
  • i need to tell the php file what file it is using? – mildlylost Dec 10 '19 at 15:49

2 Answers2

1

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>
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
  • This helps as it gives me a place to begin, I figured XML would be needed but was not sure thank you. If I find how to do this through a form I will post it back into the question. – mildlylost Dec 10 '19 at 16:39
  • Erm, that is done `"through a form"` [ I simply hardcoded a value into the form field for purposes of this demo ] - the form posts the URL to, on this occasion, the same page but could be your `rssfeed.php` script. Presumably your `rssfeed.php` script would process and display? If yes, you cld utilise the PHP code from above as basis for the `rssfeed.php` script... – Professor Abronsius Dec 10 '19 at 18:14
  • so I did separate them in the site, but this one only displays the headings for the titles of the articles, what would you use to display the whole feed in html? I am looking on W3 schools and playing with it but nothing so far. it is located at http://eleanor.codes/week14 – mildlylost Dec 10 '19 at 23:26
0

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>";
};
        ?>
mildlylost
  • 57
  • 6
  • to use `file_get_contents` with SSL enable links ( not all ) you need to use the `context` argument to that function and configure it for ssl ~ this might help. https://stackoverflow.com/questions/26148701/file-get-contents-ssl-operation-failed-with-code-1-failed-to-enable-crypto – Professor Abronsius Dec 11 '19 at 07:43