使用php脚本将产品prestashop 1.7.2.4导出到xml

时间:2018-05-21 17:30:23

标签: php xml dom mysqli export

我正在使用php脚本将所有产品从prestashop 1.7.4.2导出到xml。 该脚本运行了几分钟&返回404错误。没有错误出现......

由于我的previous post

中的Nick,sql查询工作正常

如何解决这个404错误? 我正在运行apache,使用php 7.0,脚本需要准备一个+ - 4500个产品的导出。

这是剧本:

//error_reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);

//ini_set
ini_set('max_execution_time' ,0);
ini_set('set_memory_limit', -1);

// Create connection
$dbconnect = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($dbconnect->connect_error)
{
    die('Connection failed: ' . $dbconnect->connect_error);
}
$query = 'SELECT
    psc5_product.id_product AS \'product_id\',
    psc5_product.ean13 AS \'ean13\',
    psc5_product.price AS \'price\',
    psc5_product.reference AS \'product_reference\',
    psc5_stock_available.quantity AS \'available_stock\',
    psc5_manufacturer.name AS \'brand\',
    psc5_product_lang.description AS \'description\',
    psc5_product_lang.name AS \'title\',
    concat("https://www.natureldeluxe.be/", psc5_category_lang.link_rewrite, "/", psc5_product_lang.link_rewrite) AS \'deeplink\',
    concat("https://www.natureldeluxe.be/", psc5_image.id_image, "-large_default/", psc5_product_lang.link_rewrite, ".jpg") AS \'imagelink\' 
FROM
    psc5_product 
    INNER JOIN
        psc5_stock_available 
        ON psc5_stock_available.id_product = psc5_product.id_product 
    INNER JOIN
        psc5_manufacturer 
        ON psc5_manufacturer.id_manufacturer = psc5_product.id_manufacturer 
    INNER JOIN
        psc5_product_lang 
        ON psc5_product_lang.id_product = psc5_product.id_product 
    INNER JOIN
        psc5_category_lang 
        ON psc5_category_lang.id_category = psc5_product.id_category_default 
    INNER JOIN
        psc5_image 
        ON psc5_image.id_product = psc5_product.id_product';
$dbresult = mysqli_query($dbconnect, $query);
// create a new XML document
$doc = new DomDocument('1.0');
// create root node
$root = $doc->createElement('root');
$root = $doc->appendChild($root);
// process one row at a time
while ($row = mysqli_fetch_assoc($dbresult))
{
    // add a child node for each field
    foreach ($row as $product_id => $product_id)
    {
        foreach ($row as $ean13 => $ean13)
        {
            foreach ($row as $price => $price)
            {
                foreach ($row as $ean13 => $ean13)
                {
                    foreach ($row as $product_reference => $product_reference)
                    {
                        foreach ($row as $available_stock => $available_stock)
                        {
                            foreach ($row as $brand => $brand)
                            {
                                foreach ($row as $description => $description)
                                {
                                    foreach ($row as $title => $title)
                                    {
                                        foreach ($row as $deeplink => $deeplink)
                                        {
                                            foreach ($row as $imagelink => $imagelink)
                                            {
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
// while
// get completed xml document
$xml_string = $doc->saveXML();
echo $xml_string;

2 个答案:

答案 0 :(得分:0)

不确定这是否是您的问题,尤其不应导致404错误。但是,创建XML的方式看起来不正确。

这应该使用SQL中的列名导出XML文件,每行都在<product>元素下...

$doc = new DomDocument('1.0');
// create root node
$root = $doc->createElement('root');
$root = $doc->appendChild($root);
while ($row = mysqli_fetch_assoc($dbresult)) {
    $newRow = $doc->createElement('product');
    foreach ( $row as $name=>$value )    {
        $element = $doc->createElement($name, $value);
        $newRow->appendChild($element);
    }
    $root->appendChild($newRow);
}
$xml_string = $doc->saveXML();
header('Content-type: application/xml');
echo $xml_string;

如果与问题无关,我很乐意删除这个答案,但希望它可能有所帮助。

答案 1 :(得分:0)

导出在prestashop 1.7.4.2上正常工作

使用php&amp; amp;简单的产品导出到xml SQL。

我将添加完整修改后的代码,以防万一需要它:

//error_reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);

//ini_set
ini_set('max_execution_time', 0);
ini_set('set_memory_limit', -1);

// Create connection
$dbconnect = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($dbconnect->connect_error) {
    die('Connection failed: ' . $dbconnect->connect_error);
}
$query    = 'SELECT
    psc5_product.id_product AS \'product_id\',
    psc5_product.ean13 AS \'ean13\',
    psc5_product.price AS \'price\',
    psc5_product.reference AS \'product_reference\',
    psc5_stock_available.quantity AS \'available_stock\',
    psc5_manufacturer.name AS \'brand\',
    psc5_product_lang.description AS \'description\',
    psc5_product_lang.name AS \'title\',
    concat("https://www.natureldeluxe.be/", psc5_category_lang.link_rewrite, "/", psc5_product_lang.link_rewrite) AS \'deeplink\',
    concat("https://www.natureldeluxe.be/", psc5_image.id_image, "-large_default/", psc5_product_lang.link_rewrite, ".jpg") AS \'imagelink\' 
FROM
    psc5_product 
    INNER JOIN
        psc5_stock_available 
        ON psc5_stock_available.id_product = psc5_product.id_product 
    INNER JOIN
        psc5_manufacturer 
        ON psc5_manufacturer.id_manufacturer = psc5_product.id_manufacturer 
    INNER JOIN
        psc5_product_lang 
        ON psc5_product_lang.id_product = psc5_product.id_product 
    INNER JOIN
        psc5_category_lang 
        ON psc5_category_lang.id_category = psc5_product.id_category_default 
    INNER JOIN
        psc5_image 
        ON psc5_image.id_product = psc5_product.id_product';
$dbresult = mysqli_query($dbconnect, $query);
// create a new XML document
$doc = new DomDocument('1.0');
// create root node
$root = $doc->createElement('root');
$root = $doc->appendChild($root);
while ($row = mysqli_fetch_assoc($dbresult)) {
    $newRow = $doc->createElement('product');
    foreach ( $row as $name=>$value )    {
        $element = $doc->createElement($name, htmlspecialchars($value));
        $newRow->appendChild($element);
    }
    $root->appendChild($newRow);
}
$xml_string = $doc->saveXML();
header('Content-type: application/xml');
echo $xml_string;