Snippets

Sebastian Berm Wefact import van CSV

Created by Sebastian Berm
<?php
ini_set("memory_limit","2048M");

class WeFactAPI
{

        private $url;
        private $responseType;
        private $apiKey;

        function __construct($url='https://www.uwdomein.nl/Pro/apiv2/api.php',$api_key='beveiligingscode'){
                $this->url                       = $url;
                $this->api_key           = $api_key;
        }

        public function sendRequest($controller, $action, $params){

                if(is_array($params)){
                        $params['api_key']              = $this->api_key;
                        $params['controller']   = $controller;
                        $params['action']               = $action;
                }

                $ch = curl_init();
                curl_setopt($ch,CURLOPT_URL, $this->url);
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_TIMEOUT,'10');
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
                $curlResp = curl_exec($ch);
                $curlError = curl_error($ch);


                if ($curlError != ''){
                        $result = array(
                                'controller' => 'invalid',
                                'action' => 'invalid',
                                'status' => 'error',
                                'date' => date('c'),
                                'errors' => array($curlError)
                        );
                }else{
                        $result = json_decode($curlResp, true);
                }

                return $result;
        }
}

function print_r_pre($data){
        echo "<pre>";
        print_r($data);
        echo "</pre>";
}


function addDebtor($row) {
        // sync met wefact

        $wefact = new WeFactAPI('https://www.uwdomein.nl/Pro/apiv2/api.php', 'beveiligingscode');


                $a = $row;
                $a['ZipCode'] = trim($a['ZipCode']);
                $result = $wefact->sendRequest('debtor', 'add', $a);
                echo "Result '{$a['DebtorCode']}: {$result['status']}\n";
                if ($result['status'] != 'success')
                {
                        var_dump($result);
                }

}

function ImportCSV2Array($filename)
{
    $row = 0;
    $col = 0;

    $handle = fopen($filename, "r");
    if ($handle)
    {
        while (($row = fgetcsv($handle, 4096, ';')) !== false)
        {
            if (empty($fields))
            {
                $fields = $row;
                continue;
            }

            foreach ($row as $k=>$value)
            {
                $results[$col][$fields[$k]] = $value;
            }
            $col++;
            unset($row);
        }
        if (!feof($handle))
        {
            echo "Error: unexpected fgets() failn";
        }
        fclose($handle);
    }

    return $results;
}





/// ---- Actual work;
$array = ImportCSV2Array('import.csv');
// CSV contains following ; seperated fields:
// DebtorCode;ActiveLogin;Username;CompanyName;LegalForm;Sex;Initials;SurName;Address;ZipCode;City;Country;EmailAddress;PhoneNumber;MobileNumber;Comment;PeriodicInvoiceDays ;InvoiceMethod ;InvoiceDataForPriceQuote ;InvoiceAuthorisation ;PaymentMail;InvoiceCollect;Mailing;Taxable

foreach($array as $element)
{
        addDebtor($element);
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.