Search code examples
phprssfeed

Getting data from rss and adding it to a mysql db


Hi i am making a arcade site, and it would be nice if you could use a gamefeed.

ived tryed all day to get data from xml files and add it to my mysql db, but i cant get it working.

This is the xml file i want to get info from:

http://www.freegamesforyourwebsite.com/feeds.php?feed=latest-games&format=rss

and the i want to put it into my db

can you please help me :-)?

i tryed this:

<?php
$feedUrl = 'http://playtomic.com/games/feed/playtomic?format=xml';
$ret = array();

// retrieve search results 
if($xml = simplexml_load_file($feedUrl)) {          
    $result["item"] = $xml->xpath("/rss/channel/item"); 

    foreach($result as $key => $attribute) { 
        $i=0; 
        foreach($attribute as $element) { 
             $ret[$i]['title'] = (string)$element->title; 
             $ret[$i]['swf'] = (string)$element->SWF; 
             $i++; 
        } 
    } 
}  

echo "<pre>";
print_r($ret); 
?>

Solution

  • from http://www.softarea51.com/tutorials/parse_rss_with_php.html

    you can always fetch the rss to an php array and do anything you want e.g. save it into a mysql db:

    <?php
    
    $doc = new DOMDocument();
    $doc->load('http://www.freegamesforyourwebsite.com/feeds.php?feed=latest-games&format=rss');
    $arrFeeds = array();
    foreach ($doc->getElementsByTagName('entry') as $node) {
        $itemRSS = array ( 
      'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
      'desc' => $node->getElementsByTagName('summary')->item(0)->nodeValue,
      'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
      'date' => $node->getElementsByTagName('published')->item(0)->nodeValue
      );
      array_push($arrFeeds, $itemRSS);
    }
    
    
    $mysqli = new mysqli('localhost', 'my_user', 'my_password', 'world');
    
    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    
    $stmt = $mysqli->prepare("INSERT INTO `rssitems` (`title`, `summary`, `link`, `published`) VALUES (?, ?, ?, ?)");
    $stmt->bind_param('ssss', $title, $summary, $link, $published);
    
    foreach( $arrFeeds as $RssItem){
        $title = $RssItem["title"];
        $summary = $RssItem["summary"];
        $link = $RssItem["link"];
        $published = $RssItem["published"];
    
        $stmt->execute();
    }
    
    $stmt->close();
    $mysqli->close();
    
    
    ?>