Yahoo Store Catalog to Google Shopping Feed PHP Script

时间:2017-10-16 18:24:07

标签: php xml csv google-shopping

我试图将我的Yahoo Store目录导入Google购物。 Yahoo目录以CSV格式导出,但它必须是XML。我正在创建一个PHP脚本来进行转换。到目前为止,我已经创建了所有适当的元素,根据需要转换名称和值,但是运费价格元素有问题。它在值之前自行关闭,即:

<g:price/>PRICE

而不是

<g:price>PRICE</g:price>

我相当肯定它与appendChild或嵌套有关,但我还在学习,这是我第一次使用PHP中的CSV或XML。我很感激任何建议或想法,以解决这个问题,并清理我的杂乱代码。

CSV示例(input.csv),只有一个产品:

id,name,caption,product-url,c4-tab4-local,c4-tab5-local,condition,orderable,price,sale-price,code,label,ship-weight
"10auodgrwibl","5 PACK Magnet for Military Style Steal Ammo Can","Our Military Ammo Can Magnets will stick to any side of a standard issue military ammo can like the one pictured. Use these to mark your can(s) so you don't have to open them up to see whats inside. Great for preppers! Each magnet measures approx. 3.54"" x 2.05"" or 90 x 52 mm

Wholesale/Dealer Inquires welcome.","http://www.example.com.com/10auodgrwibl.html","https://sep.yimg.com/ca/I/yhst-057809732878263172244949_2487_75897626","Ammo Can Magnets","New","Yes","12.98","4.99","PI-ACM10MMOD","Example Manufacturer","0.07"

PHP:

<?php
// Set Store Info here
$storetitle = "Example Store"; // Store/business name
$url = "http://www.example.com"; // store URL
$tagline = "Online Store"; // store tagline/catch phrase
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', true);
ini_set('auto_detect_line_endings', true);

$inputFilename    = 'input.csv';
$outputFilename   = 'output.xml';

// Open csv to read
$inputFile  = fopen($inputFilename, 'rt');

// Get the headers of the file
$headers = fgetcsv($inputFile);

// Create a new dom document with pretty formatting
$doc  = new DomDocument('1.0', 'utf-8');
$doc->formatOutput   = true;

// Add a root node to the document
$root = $doc->createElement('rss');
$root = $doc->appendChild($root);
$root->setAttribute("version", "2.0");
$root->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:g', 'http://base.google.com/ns/1.0');
$root->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:c', 'http://base.google.com/cns/1.0');

// Add channel node to the document
$channel = $doc->createElement('channel');
$root->appendChild($channel);

// Add title
$title = $doc->createElement('title');
$value = $doc->createCDATASection($storetitle);
$value = $title->appendChild($value);
$channel->appendChild($title);

// Add URL
$link = $doc->createElement('link');
$value = $doc->createCDATASection($url);
$value = $link->appendChild($value);
$channel->appendChild($link);

// Add description
$desc = $doc->createElement('description');
$value = $doc->createCDATASection($tagline);
$value = $desc->appendChild($value);
$channel->appendChild($desc);

// Loop through each row creating a <row> node with the correct data
while (($row = fgetcsv($inputFile)) !== FALSE)
{
    $container = $doc->createElement('item');
    foreach($headers as $i => $header)
    {
        if($row[4] == NULL){}
        else {      
            if($header == "id"){$child = $doc->createElement('g:id');}
            elseif($header == "name"){$child = $doc->createElement('title');}
            elseif($header == "caption"){$child = $doc->createElement('description');}
            elseif($header == "product-url"){$child = $doc->createElement('link');}
            elseif($header == "c4-tab5-local"){$child = $doc->createElement('g:product_type');}
            elseif($header == "c4-tab4-local"){$child = $doc->createElement('g:image_link');}
            elseif($header == "condition"){$child = $doc->createElement('g:condition');}
            elseif($header == "orderable"){$child = $doc->createElement('g:availability');}
            elseif($header == "price"){$child = $doc->createElement('g:price');}
            elseif($header == "sale-price"){$child = $doc->createElement('g:sale_price');}
            elseif($header == "code"){$child = $doc->createElement('g:mpn');}
            elseif($header == "label"){$child = $doc->createElement('g:brand');}
            elseif($header == "ship-weight"){$child = $doc->createElement('g:shipping');}
            else{$child = $doc->createElement($header);}

            $child = $container->appendChild($child);
            if ($header == "price" || $header == "sale-price"){$value = $doc->createCDATASection($row[$i] . " USD");}
            elseif ($header == "orderable"){
                if ($row[$i] == "Yes"){$value = $doc->createCDATASection('in stock');}
                else{$value = $doc->createCDATASection('out of stock');}
            }
            elseif ($header == "ship-weight"){
                $weight = $row[$i];

                $country = $doc->createElement('g:country');
                $value = $doc->createTextNode('US');
                $value = $country->appendChild($value);
                $child->appendChild($country);

                $method = $doc->createElement('g:service');
                $value = $doc->createTextNode('Ground');
                $value = $method->appendChild($value);
                $child->appendChild($method);

                $shipcost = $doc->createElement('g:price');
                if($weight <= "1"){$value = $doc->createTextNode('6.95 USD');}
                else{
                    $math = ceil($weight) *2 + 6.95;
                    $value = $doc->createTextNode($math . ' USD');
                }
                $value = $shipcost->appendChild($value);
                $child->appendChild($shipcost);
            }
            else{$value = $doc->createCDATASection($row[$i]);}
            $value = $child->appendChild($value);
        }
    }
    $channel->appendChild($container);
}

$strxml = $doc->saveXML();
$handle = fopen($outputFilename, "w");
fwrite($handle, $strxml);
fclose($handle);

?>

1 个答案:

答案 0 :(得分:0)

很高兴您能够解决此问题。通常,从CSV生成XML相当困难。 Google购物还接受CSV文件,因此您可以使用简单的fgetcsv和fputcsv重新映射Yahoo目录CSV导出文件。这是一个简短的示例,可以为您提供想法:

// Read lines from Yahoo CSV file
while($row = fgetcsv($fp_in))
{
  // Strip HTML from caption
  $row[2] = strip_tags($row[2]);
  // Write line to Google Shopping CSV feed file
  fputcsv($fp_out);
}

作为参考,您可以阅读此Perl script to generate a Google Shopping feed for Yahoo Store中的代码,并根据专业生成的供稿验证输出,您可以尝试使用此free Google Shopping feed generator for Yahoo Stores