1

我正在为我的 magento 副本编写一个 PHP 导出脚本。出于某种原因,以下代码给了我一个“标头已发送”错误:

//Load magento and set to match frontend
require_once '../../app/Mage.php';
umask(0);
Mage::app();
Mage::app()->loadArea(Mage_Core_Model_App_Area::AREA_FRONTEND);

//Send headers to browser to prep for csv file download
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=exportSKUs.csv');


//Load only product collection details that we need.
$product    = Mage::getModel('catalog/product');
$product->getCollection()->getSelect()->limit(5);
$products   = $product->getCollection()
    ->addFieldToFilter('status','1')
    ->addAttributeToSelect('name')
    ->addAttributeToSelect('sku')
    ->addAttributeToSelect('upc')
    ->addAttributeToSelect('status')
    ->addAttributeToSelect('price')
    ->addAttributeToSelect('special_price')
    ->addAttributeToSelect('description')
    ->addAttributeToSelect('category_ids')
    ->addAttributeToSelect('short_description');

//Open current output to fputcsv
$fp = fopen('php://output', 'w');

//CSV headers
$headerRow = array(
    'name',
    'sku',
    'upc',
    'status',
    'price',
    'special_price',
    'description',
    'category_ids',
    'short_description'
);
fputcsv($fp, $headerRow);
$count = 0;
//CSV Rows
foreach($products as &$product){
    $categoryIds = implode(',', $product->getCategoryIds());
    $row = array(
        $product->getName(),
        $product->getSku(),
        $product->getUpc(),
        $product->getStatus(),
        $product->getPrice(),
        $product->getSpecialPrice(),
        $product->getDescription(),
        $categoryIds,
        $product->getShortDescription()
    );
    fputcsv($fp, $row);
    $count++;
    if($count>5){
        //Close current output (save csv)
        fclose($fp);
        exit;
    }
}

这里引起我问题的代码行是这样的:fputcsv($fp, $headerRow); 由于某种原因,当这行被注释掉时,脚本运行良好。但是,当此行与脚本一起运行时,它会触发标头已发送错误。我不明白为什么我能够在我的 foreach 循环内多次运行 fputcsv(fputcsv($fp, $row);)但我根本无法在 foreach 循环之前运行它。

我有解决这个问题的方法,所以它不是超级关键,但我真的希望我能理解这里发生了什么导致这个问题。

谢谢你的时间!

4

1 回答 1

1

我已经更改了您的代码...检查一下。我使用magento进程导出...

<?php
//Load magento and set to match frontend
require_once '../../app/Mage.php';
umask(0);
Mage::app();
Mage::app()->loadArea(Mage_Core_Model_App_Area::AREA_FRONTEND);

//Send headers to browser to prep for csv file download
//header('Content-Type: text/csv');
//header('Content-Disposition: attachment;filename=exportSKUs.csv');
//
$filename="exportSKUs.csv";


//Load only product collection details that we need.
$product    = Mage::getModel('catalog/product');
$product->getCollection()->getSelect()->limit(5);
$products   = $product->getCollection()
    ->addFieldToFilter('status','1')
    ->addAttributeToSelect('name')
    ->addAttributeToSelect('sku')
    ->addAttributeToSelect('upc')
    ->addAttributeToSelect('status')
    ->addAttributeToSelect('price')
    ->addAttributeToSelect('special_price')
    ->addAttributeToSelect('description')
    ->addAttributeToSelect('category_ids')
    ->addAttributeToSelect('short_description');


 $io = new Varien_Io_File();
                $path = Mage::getBaseDir('var') . DS . 'export' . DS;
                $name = md5(microtime());
                $file = $path . DS . $name . '.csv';
                $io->setAllowCreateFolders(true);
                $io->open(array('path' => $path));
                $io->streamOpen($filename, 'w+');
                $io->streamLock(true);
                $headerRow = array(
                    'name',
                    'sku',
                    'upc',
                    'status',
                    'price',
                    'special_price',
                    'description',
                    'category_ids',
                    'short_description'
                );
                $io->streamWriteCsv($headerRow);

                    foreach($products as &$product){
                    $categoryIds = implode(',', $product->getCategoryIds());
                    $row = array(
                    $product->getName(),
                    $product->getSku(),
                    $product->getUpc(),
                    $product->getStatus(),
                    $product->getPrice(),
                    $product->getSpecialPrice(),
                    $product->getDescription(),
                    $categoryIds,
                    $product->getShortDescription()
                    );
                    $io->streamWriteCsv($row);
                }
?>
于 2014-04-12T07:32:44.990 回答