XML文件未正确显示Google XML Live Data Feed的数据

时间:2015-02-04 13:29:22

标签: php mysql xml

我正在尝试使用PHP语言为Google Live数据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文件的错误,以及为什么数据没有以我想要的精确方式显示。

1 个答案:

答案 0 :(得分:0)

您生成的XML无效。你没有逃避变量,所以他们可以打破它。通道和rss的结束标签也缺失了。使用XML API,如XMLWriter或DOM。