Snippets
Created by
Sebastian Berm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | <?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)
You can clone a snippet to your computer for local editing. Learn more.