从rss获取数据并将其添加到mysql数据库

时间:2012-01-26 22:42:15

标签: php rss feed

您好我正在制作一个街机游戏网站,如果您可以使用游戏源,那将会很不错。

ived整日尝试从xml文件中获取数据并将其添加到我的mysql数据库中,但我无法使其正常工作。

这是我想从中获取信息的xml文件:

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

我希望将它放入我的数据库

你可以帮助我: - )?

我试过这个:

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

2 个答案:

答案 0 :(得分:1)

来自http://www.softarea51.com/tutorials/parse_rss_with_php.html

你总是可以将rss提取到php数组并做你想做的任何事情,例如将它保存到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();


?>

答案 1 :(得分:0)

此代码对我来说非常合适..

$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>';
    }

如果要将数据插入数据库, 根据您的要求创建数据库表和字段,并在for循环中运行插入查询。 Click here to see the image of Rss Feeds Display