嗨,我正在制作一个街机网站,如果你能使用游戏提要,那就太好了。
我试了一整天从xml文件中获取数据并将其添加到我的mysqldb中,但我无法使其工作。
这是我想从中获取信息的xml文件:
http://www.freegamesforyourwebsite.com/feeds.php?feed=latest-游戏&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);
?>
来自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();
?>
这段代码对我来说运行得很好
$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(' & ', ' & ', $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>';
}
如果您想将数据插入数据库,根据您的要求创建DB表和字段,并在for循环中运行插入查询单击此处查看Rss Feeds Display 的图像