RSS未定义的项目元素

时间:2014-03-17 16:51:25

标签: php xml rss

当我尝试validate我的RSS feed时,我收到以下两个错误:

line 2, column 359: Undefined item element: shopName (36 occurrences) [help]
... /Prod/pgeproduct.aspx?iid=3100539</guid><shopName>ASOS</shopName><photoU ...
                                             ^
line 2, column 384: Undefined item element: photoURL (36 occurrences) [help]
... =3100539</guid><shopName>ASOS</shopName><photoURL>http://images.asos-med ...

知道为什么吗?

这是我用来创建Feed的代码:

<?php

function scrape($list_url, $shop_name, $photo_location, $photo_url_root, $product_location, $product_url_root, $was_price_location, $now_price_location, $gender, $country)
{
    header("Content-Type: application/xml; charset=UTF-8");
    $xml = new SimpleXMLElement('<rss/>');
    $xml->addAttribute("version", "2.0");
    //$xml->addAttribute("xmlns:atom", "http://www.comfyshoulderrest.com/scrape.php");
    $channel = $xml->addChild("channel");
    $channel->addChild("title", "Shopaholic");
    $channel->addChild("description", "The RSS feed for Shopaholic");
    $channel->addChild("link", "http://www.sebastiansalek.com");

    $html = file_get_contents($list_url);
    $doc = new DOMDocument();
    libxml_use_internal_errors(TRUE);

    if(!empty($html))
    {
        $doc->loadHTML($html);
        libxml_clear_errors(); // remove errors for yucky html
        $xpath = new DOMXPath($doc);

        /* FIND LINK TO PRODUCT PAGE */

        $products = array();

        $row = $xpath->query($product_location);

        /* Create an array containing products */
        if ($row->length > 0)
        {            
            foreach ($row as $location)
            {
                $product_urls[] = $product_url_root . $location->getAttribute('href');
            }
        }

        $imgs = $xpath->query($photo_location);

        /* Create an array containing the image links */
        if ($imgs->length > 0)
        {            
            foreach ($imgs as $img)
            {
                $photo_url[] = $photo_url_root . $img->getAttribute('src');
            }
        }

        $result = array();

        /* Create an associative array containing all the above values */
        foreach ($product_urls as $i => $product_url)
        {
            $result = array(
                'product_url' => $product_url,
                'shop_name' => $shop_name,
                'photo_url' => $photo_url[$i]
            );

            $item = $channel->addChild("item");
            $item->addChild("title", $result['product_url']);
            $item->addChild("guid", $result['product_url']);
            $item->addChild("shopName", $result['shop_name']);
            $item->addChild("photoURL", $result['photo_url']);
            //print_r($result);
        }
    }
    else
    {
        echo "this is empty";
    }
    echo $xml->asXML();
}

/* CONNECT TO DATABASE */

$dbhost = "xxx";
$dbname = "xxx";
$dbuser = "xxx";
$dbpass = "xxx";

$con = mysqli_connect("$dbhost", "$dbuser", "$dbpass", "$dbname");

if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$id = $_GET['id'];

/* GET FIELDS FROM DATABASE */

$result = mysqli_query($con, "SELECT * FROM scrape WHERE id = '$id'");

while($row = mysqli_fetch_array($result))
{   
    $list_url = $row['list_url'];
    $shop_name = $row['shop_name'];
    $photo_location = $row['photo_location'];
    $photo_url_root = $row['photo_url_root'];
    $product_location = $row['product_location'];
    $product_url_root = $row['product_url_root'];
    $was_price_location - $row['was_price_location'];
    $now_price_location - $row['now_price_location'];
    $gender = $row['gender'];
    $country = $row['country'];
}

scrape($list_url, $shop_name, $photo_location, $photo_url_root, $product_location, $product_url_root, $was_price_location, $now_price_location, $gender, $country);

mysqli_close($con);

?>

0 个答案:

没有答案