gpt4 book ai didi

php - XML 文件无法正确显示 Google XML Live Data Feed 的数据

转载 作者:行者123 更新时间:2023-11-29 23:05:10 25 4
gpt4 key购买 nike

我正在尝试使用 PHP 语言为 google live data feed 创建一个 xml 文件。但问题是我的 xml 文件无法正确显示内容。我猜我的 xml 文件无效。在 Firefox 中,仅显示来自 mysql 数据库的两个字段,如下所示 -

example.com
Google Merchant Feed
PM-3114-100 Smart Power Meter Single phase, 4 loops (60A)

Compact Single Phase Smart Power Meter, 4 loops, ModbusRTU protocol. 60A CT's.
050-063 62 ohm precision shunt resistor for 4-20ma transducer

62 Ω precision shunt resistor for use when monitoring 4-20mA transducers with Datascan 7000 modules. One required for each channel where 4-20ma transducers are required.

在chrome和IE中,没有数据渲染,只显示错误。在 Chrome 中,我收到以下错误 -

This page contains the following errors:

error on line 48 at column 9: Extra content at the end of the document
Below is a rendering of the page up to the first error.

这是我创建 xml 文件的 PHP 代码 -

<?php
// Set the xml header
header("Content-type: text/xml");

require_once '../admin/includes/login.php';
$sql = "SELECT `p`.`products_id`, `p`.`products_name`, `p`.`products_url`, `p`.`products_short_desc`, `p`.`products_model`, `p`.`products_price`, `p`.`products_status`, `p`.`products_image`, `c`.`categories_name`, `b`.`brand_name` FROM `products` AS `p` LEFT JOIN `categories` AS `c` ON `p`.`categories_id` = `c`.`categories_id` LEFT JOIN `brands` AS `b` ON `p`.`brand_id` = `b`.`brand_id` LIMIT 1, 2";

$rs = mysqli_query($con, $sql);


// Echo out all the details
echo '<?xml version="1.0"?>
<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<channel>
<title>example.com</title>
<link>http://www.example.com/</link>
<description>Google Merchant Feed</description>';

// while loop, this will cycle through the products and echo out all the variables
while($row = mysqli_fetch_array($rs))
{
$sql2 = "SELECT `categories_id`,`products_url`,`products_name` FROM `products` WHERE `products_id` = " . $row['products_id'];
$rs2 = mysqli_query($con, $sql2);
$row2 = mysqli_fetch_assoc($rs2);
$categories_id = $row2['categories_id'];
$structure = structure($categories_id);
$structure = array_reverse($structure);
$structure[] = $categories_id;

$breadcrumb = '';
$sub_url = $base;

foreach($structure as $url) {
$sql3 = mysqli_query($con, "SELECT `categories_name`,`categories_url` FROM `categories` WHERE `categories_id` = $url");
$row3 = mysqli_fetch_assoc($sql3);
if($url==0) {
$breadcrumb .= '<a id="breadcrumb_home" href="'.$sub_url.$row3['categories_url'].'">HOME > </a>';
} else {
$breadcrumb .= '<a class="breadcrumb_link" href="'.$sub_url.$row3['categories_url'].'">'.$row3['categories_name'].' > </a>';
$sub_url = $sub_url.preg_replace("/_c[0-9_]+/", "", $row3['categories_url']).'/';
}
}

// collect all variables
$brand = $row['brand_name'];
$title = $row['products_name'];
$link = $sub_url . $row['products_url'];
$description = $row['products_short_desc'];
$id = $row['products_model'];
//$condition = $row['products_condition'];
$condition = "new";
$price = $row['products_price'];
if($row['products_status'] == 1){$availability = 'in stock';} else {$availability = 'out of stock';}
$products_image = $row['products_image'];
$image = 'http://www.example.com/images/products/page/' . $products_image;
$category = $row['categories_name'];
//$gtin = $row['GTIN'];
//$mpn = $row['MPN'];
$gtin = 8808992787426;
$mpn = "M2262D-PC";
// output all variables into the correct google tags
echo "<item>
<title>$title</title>
<link>$link</link>
<description><![CDATA[$description]]></description>
<g:google_product_category>$category</g:google_product_category>
<g:id>$id</g:id>
<g:condition>$condition</g:condition>
<g:price>$price GBP</g:price>
<g:availability>$availability</g:availability>
<g:image_link><![CDATA[$image]]></g:image_link>
<g:shipping>
<g:country>UK</g:country>
<g:service>Standard</g:service>
<g:price>5.95 GBP</g:price>
</g:shipping>
<g:gtin>$gtin</g:gtin>
<g:brand>$brand</g:brand>
<g:mpn>$mpn</g:mpn>
<g:product_type>$category</g:product_type>
</item>";
}
?>

我正在尽最大努力调查该问题,但无法追踪它。请帮助我找出错误,为什么没有创建正确的 xml 文件以及为什么数据没有按照我想要的方式显示。

最佳答案

您生成的 XML 无效。您不会转义变量,因此它们可以破坏它。 channel 和 rss 的结束标签也丢失了。使用 XMLWriter 或 DOM 等 XML API。

关于php - XML 文件无法正确显示 Google XML Live Data Feed 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28322664/

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