【问题标题】:Getting data from rss and adding it to a mysql db从 rss 获取数据并将其添加到 mysql 数据库
【发布时间】:2012-01-26 22:42:14
【问题描述】:

您好,我正在制作一个街机网站,如果您可以使用游戏源,那就太好了。

我整天都在尝试从 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); 
?>

【问题讨论】:

  • 好的,只要告诉我们你做了什么,我们就会知道为什么它不起作用

标签: php rss feed


【解决方案1】:

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

您可以随时将 rss 提取到 php 数组并执行任何您想做的事情,例如将其保存到 mysql 数据库中:

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


?>

【讨论】:

  • 立即查看。现在可以了。在第 6 行中将 item 更改为 entry
  • 好的,还修复了“日期”和“摘要”元素。现在它运行没有任何警告
  • 如果你想查看数组的内容,你必须像这样使用var_dumpvar_dump($arrFeeds);
  • 哇!它起作用了:D! - 在将其添加到数据库时寻求一些帮助可能是不是太过分了 :-)??
  • 好的。在这里,您可以使用 db 代码。您应该在 db 中有一个带有相应列的 rssitems 表,并且也不要更改此行中的 db 连接信息:$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'world');
【解决方案2】:

这段代码对我来说工作得很好..

$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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-27
    • 2020-07-31
    • 2013-03-02
    • 2021-10-15
    • 2022-07-28
    • 1970-01-01
    • 1970-01-01
    • 2017-01-15
    相关资源
    最近更新 更多