我正在尝试做的是使用 PHP 的 simpleXML 函数将 RSS XML 提要导入 MySQL 数据库。但是 PHP 脚本所做的唯一一件事就是将 0 写入我的数据库。我做错了什么?
示例 rss.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml-stylesheet title="XSL_formatting" type="text/xsl" href="/divers/rss/xslt.php" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title></title>
<link></link>
<description></description>
<lastBuildDate></lastBuildDate>
<ttl></ttl>
<item>
<title></title>
<description></description>
<link></link>
<guid></guid>
<pubDate></pubDate>
</item>
</channel>
</rss>
PHP
<?php
include ('config.php');
$xml_file = "http://xml.xml/rss.xml";
$xml_load = simplexml_load_string(file_get_contents($xml_file));
$link = (string)$xml_load->link;
$guid = (string)$xml_load->guid;
$sql = "INSERT INTO table (link,guid) VALUES('$link','$guid');";
mysql_query($sql) or die("Error ".mysql_error());
?>
最佳答案
这是我在做的:
$sxml = simplexml_load_file($feedURL); // loads in simple xml format
foreach ($sxml->entry as $entry) {
$title = stripslashes($entry->title);
$summary = stripslashes($entry->summary);
}
如您所见,我使用了 stripsplashes()
。
我猜它看起来像:
foreach($xml->item as $item) {
$link = stripslashes($item->link);
$guid = stripslashes($item->guid);
}
关于php - 如何将特定标签从 RSS XML 导入 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14175720/