#!/usr/bin/php
<?php
/**
 * This Script gets all products from OXID eShop and writes into one csv file.
 *
 * Lineformat is exactly as described in ERP/CSV documentation
 *
 * Example: Getting SID
 * http://testshops/EE/modules/erp/oxerpcsvexport.php?fnc=OXERPLogin&user=admin&pw=admin&version=2.2.0&shopid=1&langid=0
 *
 * Example: Getting all products (=articles)
 * http://testshops/EE/modules/erp/oxerpcsvexport.php?fnc=OXERPGetArticles&sid=2b53f75583dea05faf970b83726f6047
 *
 * @link http://www.oxid-esales.com
 * @package  ERP/CSV
 * @copyright © OXID eSales AG 2003-2009
 * @version 1.0
 *
 */

// URL to Shop
define ('ERPSCVURL', 'http://testshops/EE/modules/erp/oxerpcsvexport.php');

// Import- and Export path
define ('EXPORTPATH', '/htdocs/testshops/EE/export');
define ('IMPORTPATH', '/htdocs/testshops/EE/import');

// Export filename
define ('EXPORTFILENAME', 'products.csv');

// Admin user data
define ('ADMINUSER', 'admin');
define ('ADMINPASS', 'admin');

// Subshop ID and Language
define ('SHOPID', '1');
define ('LANGID', '0');

/**
 * Logs in and gets Session ID (SID)
 *
 * @return string session id
 */
function getSID ()
{

    $sUrl = ERPSCVURL.'?fnc=OXERPLogin&user='.ADMINUSER.'&pw='.ADMINPASS.'&version=2.2.0&shopid='.SHOPID.'&langid='.LANGID;

    if ($fp = fopen ($sUrl, 'r')) {
        $sid = fgets($fp, 2048);
        fclose ($fp);
    }

    return $sid;
}


/**
 * Writes all products into one file
 *
 * @param string $sSid Session ID
 *
 * @return nothing
 */
function writeArticles ($sSid)
{
    if ($fp_out = fopen (EXPORTPATH.'/'.EXPORTFILENAME, 'w') ) {

        $sUrl = ERPSCVURL.'?fnc=OXERPGetArticles&sid='.$sSid;

        if ($fp_in = fopen ($sUrl, 'r')) {
            while ($line = fgets ($fp_in, 10240) ) {
                fputs($fp_out, $line);
            }
            fclose ($fp_in);
        }
    }
}

writeArticles(GetSID());


