php - 如何将特定标签从 RSS XML 导入 MySQL 数据库

标签 php mysql import simplexml

我正在尝试做的是使用 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/

相关文章:

php - 需要一个任意 PHP 文件,而不会将变量泄漏到作用域中

php - 使用两行脚本记住页面位置

php - 如何将模板页脚添加到wordpress中的所有页面

mysql - 如何选择具有偏移量的特定行

mysql - 使用内连接语句创建 sql View

PostgreSQL COPY csv 包括引号

PHP:为什么! !"0.00"结果为真

mysql - 使用 Mysql2 gem 的 Ruby SQL 插入

python - 在 python 中的文件之间共享 header ?

r - 如何在 R 中将.table() 多个文件读入一个表中?