0

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); 
?>
1
  • Sure, just show us what you have done and we will see why is it not working Commented Jan 26, 2012 at 22:46

2 Answers 2

3

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();


?>
Sign up to request clarification or add additional context in comments.

13 Comments

check it now. it now works. changed item to entry in line 6
okay fixed also the "date" and "summary" elements. now it run without any warning
if you want to see the content of an array you've got to use var_dump like this: var_dump($arrFeeds);
Wow ty! it worked :D! - could it be to much to ask for some help with adding it to the db :-)??
OK. here you go with db code. you should have a rssitems table in db with corresponding columns and also don't for get to changed the db connection info in this line: $mysqli = new mysqli('localhost', 'my_user', 'my_password', 'world');
|
0

This code is working perfectly fine for me ..

$rss = new DOMDocument();
    $rss->load('http://www.hamarakhana.com/feed/');
    $feed = array();
    foreach ($rss->getElementsByTagName('item') as $node) {
        $item = array ( 
            'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
            'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
            'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
            'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
            );
        array_push($feed, $item);
    }
    $limit = 10;
    for($x=0;$x<$limit;$x++) {
        $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
        $link = $feed[$x]['link'];
        $description = $feed[$x]['desc'];
        $date = date('l F d, Y', strtotime($feed[$x]['date']));
        echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
        echo '<small><em>Posted on '.$date.'</em></small></p>';
        echo '<p>'.$description.'</p>';
    }

if you want to insert data into database , create your DB table and fields according to your requiremnt and run the insert query in for loop.. Click here to see the image of Rss Feeds Display

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.