gpt4 book ai didi

php - 从另一个域获取 XML 文件到 Mysql 数据库

转载 作者:行者123 更新时间:2023-11-29 18:05:43 26 4
gpt4 key购买 nike

我正在尝试通过 php 脚本从要导入到 mysql 的 url 获取 xml 信息,但我遇到了一些麻烦,而且我的经验并未涵盖此领域。XML 的构成如下例所示:

 <?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0" xmlns:g="http://base.google.se/ns/1.0">
<channel>
<title></title>
<description></description>
<link></link>
<item>
<g:id></g:id>
<title></title>
<g:product></g:product>
</item>
<item>
<g:id></g:id>
<title></title>
<g:product></g:product>
</item>
and so on...

使用 php 脚本:

<?php

include '../connection-to-db.php';

$str_xml = file_get_contents('http://www.example.com/xmls/xmlfile.xml');
$library = new SimpleXMLElement($str_xml);

$arr = json_decode( json_encode($library) , true);
var_dump ($arr);

echo "Array got " .sizeof($arr['item']) . " items.<br> <br>";
if (sizeof($arr['item']) > 155555500) {

mysql_query("TRUNCATE TABLE google_stat");

$count = 0;
foreach ($arr['item'] as $shelf)
{
$gId = mysql_real_escape_string($shelf['g:id']);
$Title = mysql_real_escape_string($shelf['title']);
$gProductType = mysql_real_escape_string($shelf['g:product']);

mysql_query("INSERT INTO google_stat (gid, title, gcategory)
VALUES ('$gID', '$Title', '$gCategory')")
or die(mysql_error());

$count ++;
}
echo " Counted: " . $count . "inserts";
} else {
echo "Non counted, no insert done";
}
?>

问题是,当 SimpleXMLElement 时,当我查看输出时,名称中带有 g: 的所有项目似乎都消失了,它甚至找不到任何项目。我什至尝试过使用具有相同 XML 树的本地文件,但甚至无法使其工作。我很感谢所提供的任何帮助,因为我越来越意识到我在这方面陷入了困境。

更新:

<?php

include '../connection-to-db.php';

$str_xml = file_get_contents('http://www.example.com/xmls/xmlfile.xml');
$library = new SimpleXMLElement($str_xml);

$arr = json_decode( json_encode($library) , true);


echo "Array got " .sizeof($library->channel->item) . " items.<br> <br>";
if (sizeof($library->channel->item) > 100) {

mysql_query("TRUNCATE TABLE google_stat");

$count = 0;
foreach ($library->channel->item as $shelf)
{
$gId = (string) $shelf->children('g', TRUE)->id;
$Title = (string) $shelf->title;
$gProductType = $shelf->children('g', TRUE)->product;

echo $gId."<br />";
echo $Title."<br />";
echo $gProductType."<br />";

$count ++;
}
echo " Counted: " . $count . "inserts";
} else {
echo "Non counted, no insert done";
}
?>

现在我得到了数组中的项目数,但 $gId、$Title 等不回显任何值。

Edit2:必须进行高数组检查,它有效。

最佳答案

它与您想要的 namespace 前缀有关。您可以像这样访问 g: 项目:

<?php

$str_xml = file_get_contents('test.xml');
$library = new SimpleXMLElement($str_xml);

$count = 0;
foreach ($library->channel->item as $shelf)
{

$gId = (string) $shelf->title;
$Title = (string) $shelf->children('g', TRUE)->id;
$gProductType = (string) $shelf->children('g', TRUE)->product;

echo $gId."<br />";
echo $Title."<br />";
echo $gProductType."<br />";

$count ++;
}
echo " Counted: " . $count . " inserts";

?>

参见https://www.sitepoint.com/simplexml-and-namespaces/以供进一步引用。

xml.测试

<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0" xmlns:g="http://base.google.se/ns/1.0">
<channel>
<title>product</title>
<description>lots of products</description>
<link>www.example.com</link>
<item>
<g:id>ID 1</g:id>
<title>Title 1</title>
<g:product>Product 1</g:product>
</item>
<item>
<g:id>ID 2</g:id>
<title>Title 2</title>
<g:product>Product 2</g:product>
</item>
</channel>
</rss>

输出:

标题 1

ID 1

产品1

标题 2

ID 2

产品2

计数:2 个插入

关于php - 从另一个域获取 XML 文件到 Mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47871547/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com