· 5 years ago · May 28, 2020, 06:48 AM
1<?php
2set_time_limit(300);
3session_start();
4
5// include ERPLY API class
6include("EAPI.class.php");
7
8// Initialise class
9$api = new EAPI();
10
11// Configuration settings
12$api->clientCode = 3280;
13$api->username = 'markopaju92@gmail.com';
14$api->password = 'Feedtest1';
15$api->url = "https://" . $api->clientCode . ".erply.com/api/";
16
17$i = 0;
18$priceListProducts = array();
19$priceListProductIDs = array();
20
21$result = $api->sendRequest("getCountries", array());
22$output = json_decode($result, true);
23if($output['status']['responseStatus'] == 'ok')
24{
25 $countries = $output['records'];
26}
27
28$result = $api->sendRequest("getProducts", array('warehouseID' => 1, 'getItemsFromFirstPriceListOnly' => 1, 'getPriceListPrices' => 1, 'getAllLanguages' => 1, 'recordsOnPage' => 1000, 'getParameters' => 1, 'getStockInfo' => 1, 'pageNo' => 1));
29$output = json_decode($result, true);
30//print_r($output);die();
31$totalPages = ceil($output['status']['recordsTotal']/$output['status']['recordsInResponse']);
32
33$products = array();
34
35for($page = 1; $page <= $totalPages; $page++)
36{
37 $result = $api->sendRequest("getProducts", array('warehouseID' => 1, 'getItemsFromFirstPriceListOnly' => 1, 'getPriceListPrices' => 1, 'getAllLanguages' => 1, 'recordsOnPage' => 1000, 'getParameters' => 1, 'getStockInfo' => 1, 'pageNo' => $page));
38 $output = json_decode($result, true);
39 $languages = array('EE','EN','RU','LV','FI');
40 foreach ($output['records'] as $product) {
41 $products['product'][$i] = array(
42 'change_date' => gmdate('c', $product['lastModified']),
43 'product_code' => $product['code'],
44 'brand' => $product['brandName'],
45 'ean' => $product['code2'],
46 'retail_price' => $product['priceWithVat'],
47 'supplier_price' => $product['price'],
48 'discount_price' => $product['priceListPrice'],
49 'descriptions' => array(
50 array(
51 'description' => array(
52 '@attributes' => array(
53 'lang' => 'EE'
54 ),
55 '@value' => $product['nameEST'],
56 )
57 )
58 ),
59
60 'category' => array(
61 '@attributes' => array(
62 'id' => $product['groupName']
63 ),
64 ),
65 );
66
67 $brandName = $product['brandName'];
68 $productCode = $product['code'];
69
70 if(@getimagesize( 'http://kellakeskus.ee/images/'.$brandName.'/'. $productCode .'.jpg'))
71 {
72 $products['product'][$i]['pictures']['picture'][] = 'http://kellakeskus.ee/images/'.$brandName.'/'. $productCode .'.jpg';
73 }
74
75 if(@getimagesize( 'http://kellakeskus.ee/images/'.$brandName.'/'. $productCode .'.png'))
76 {
77 $products['product'][$i]['pictures']['picture'][] = 'http://kellakeskus.ee/images/'.$brandName.'/'. $productCode .'.png';
78 }
79
80
81 //extra pildid
82 if(@getimagesize( 'http://kellakeskus.ee/images/'.$brandName.'/'. $productCode .'_1.jpg'))
83 {
84 $products['product'][$i]['pictures']['picture'][] = 'http://kellakeskus.ee/images/'.$brandName.'/'. $productCode .'_1.jpg';
85 }
86 if(@getimagesize( 'http://kellakeskus.ee/images/'.$brandName.'/'. $productCode .'_2.jpg'))
87 {
88 $products['product'][$i]['pictures']['picture'][] = 'http://kellakeskus.ee/images/'.$brandName.'/'. $productCode .'_2.jpg';
89 }
90 if(@getimagesize( 'http://kellakeskus.ee/images/'.$brandName.'/'. $productCode .'_3.jpg'))
91 {
92 $products['product'][$i]['pictures']['picture'][] = 'http://kellakeskus.ee/images/'.$brandName.'/'. $productCode .'_3.jpg';
93 }
94 if(@getimagesize( 'http://kellakeskus.ee/images/'.$brandName.'/'. $productCode .'_4.jpg'))
95 {
96 $products['product'][$i]['pictures']['picture'][] = 'http://kellakeskus.ee/images/'.$brandName.'/'. $productCode .'_4.jpg';
97 }
98 if(@getimagesize( 'http://kellakeskus.ee/images/'.$brandName.'/'. $productCode .'_5.jpg'))
99 {
100 $products['product'][$i]['pictures']['picture'][] = 'http://kellakeskus.ee/images/'.$brandName.'/'. $productCode .'_5.jpg';
101 }
102
103 if(@getimagesize( 'http://kellakeskus.ee/images/'.$brandName.'/'. $productCode .'_1.png'))
104 {
105 $products['product'][$i]['pictures']['picture'][] = 'http://kellakeskus.ee/images/'.$brandName.'/'. $productCode .'_1.png';
106 }
107 if(@getimagesize( 'http://kellakeskus.ee/images/'.$brandName.'/'. $productCode .'_2.png'))
108 {
109 $products['product'][$i]['pictures']['picture'][] = 'http://kellakeskus.ee/images/'.$brandName.'/'. $productCode .'_2.png';
110 }
111 if(@getimagesize( 'http://kellakeskus.ee/images/'.$brandName.'/'. $productCode .'_3.png'))
112 {
113 $products['product'][$i]['pictures']['picture'][] = 'http://kellakeskus.ee/images/'.$brandName.'/'. $productCode .'_3.png';
114 }
115 if(@getimagesize( 'http://kellakeskus.ee/images/'.$brandName.'/'. $productCode .'_4.png'))
116 {
117 $products['product'][$i]['pictures']['picture'][] = 'http://kellakeskus.ee/images/'.$brandName.'/'. $productCode .'_4.png';
118 }
119 if(@getimagesize( 'http://kellakeskus.ee/images/'.$brandName.'/'. $productCode .'_5.png'))
120 {
121 $products['product'][$i]['pictures']['picture'][] = 'http://kellakeskus.ee/images/'.$brandName.'/'. $productCode .'_5.png';
122 }
123
124
125 if(isset($product['warehouses']))
126 {
127 foreach($product['warehouses'] as $warehouse)
128 {
129 if($warehouse['warehouseID'] == '1')
130 {
131 if($warehouse['free'] <= 10)
132 {
133 $products['product'][$i]['stock'] = $warehouse['free'];
134 } else {
135 $products['product'][$i]['stock'] = '10+';
136 }
137
138 }
139 }
140 }
141
142 foreach($languages as $language)
143 {
144 if($language == 'EE')
145 {
146 foreach($product['parameters'] as $productParameter)
147 {
148 if($productParameter['parameterName'] == 'Nimetus EST')
149 {
150 $products['product'][$i]['names']['name'][] = array(
151 '@attributes' => array(
152 'lang' => $language
153 ),
154 '@value' => $productParameter['parameterValue']
155 );
156 }
157 }
158
159 } elseif($language == 'FI')
160 {
161 foreach($product['parameters'] as $productParameter)
162 {
163 if($productParameter['parameterName'] == 'Nimetus FIN')
164 {
165 $products['product'][$i]['names']['name'][] = array(
166 '@attributes' => array(
167 'lang' => $language
168 ),
169 '@value' => $productParameter['parameterValue']
170 );
171 }
172 }
173 }elseif($language == 'EN')
174 {
175 $products['product'][$i]['names']['name'][] = array(
176 '@attributes' => array(
177 'lang' => $language
178 ),
179 '@value' => $product['nameENG']
180 );
181 }elseif($language == 'LV')
182 {
183 $products['product'][$i]['names']['name'][] = array(
184 '@attributes' => array(
185 'lang' => $language
186 ),
187 '@value' => $product['nameLAT']
188 );
189 }elseif($language == 'RU')
190 {
191 $products['product'][$i]['names']['name'][] = array(
192 '@attributes' => array(
193 'lang' => $language
194 ),
195 '@value' => $product['nameRUS']
196 );
197 }
198
199 }
200
201 if (isset($product['groupName'])) {
202 $products['product'][$i]['attributes']['attribute'][] = array(
203
204 '@attributes' => array(
205 'type' => 'text'
206 ),
207 'attribute_name' => 'Kategooria',
208 'attribute_value' => $product['groupName']
209 );
210 }
211
212 if (isset($product['netWeight'])) {
213 $products['product'][$i]['attributes']['attribute'][] = array(
214
215 '@attributes' => array(
216 'type' => 'text'
217 ),
218 'attribute_name' => 'Kaal',
219 'attribute_value' => $product['netWeight']
220 );
221 }
222
223 if (isset($product['grossWeight'])) {
224 $products['product'][$i]['attributes']['attribute'][] = array(
225
226 '@attributes' => array(
227 'type' => 'text'
228 ),
229 'attribute_name' => 'BrutoKaal',
230 'attribute_value' => $product['grossWeight']
231 );
232 }
233
234 if (isset($product['extraField1Name'])) {
235 $products['product'][$i]['attributes']['attribute'][] = array(
236
237 '@attributes' => array(
238 'type' => 'text'
239 ),
240 'attribute_name' => $product['extraField1Code'],
241 'attribute_value' => $product['extraField1Name']
242 );
243 }
244
245 if (isset($product['extraField2Name'])) {
246 $products['product'][$i]['attributes']['attribute'][] = array(
247
248 '@attributes' => array(
249 'type' => 'text'
250 ),
251 'attribute_name' => $product['extraField2Code'],
252 'attribute_value' => $product['extraField2Name']
253 );
254 }
255
256 if (isset($product['extraField3Name'])) {
257 $products['product'][$i]['attributes']['attribute'][] = array(
258
259 '@attributes' => array(
260 'type' => 'text'
261 ),
262 'attribute_name' => $product['extraField3Code'],
263 'attribute_value' => $product['extraField3Name']
264 );
265 }
266
267 if (isset($product['extraField4Name'])) {
268 $products['product'][$i]['attributes']['attribute'][] = array(
269
270 '@attributes' => array(
271 'type' => 'text'
272 ),
273 'attribute_name' => $product['extraField4Code'],
274 'attribute_value' => $product['extraField4Name']
275 );
276 }
277
278 //TOLLIKOOD
279 if (isset($product['code3'])) {
280 $products['product'][$i]['attributes']['attribute'][] = array(
281
282 '@attributes' => array(
283 'type' => 'text'
284 ),
285 'attribute_name' => 'Tollikood',
286 'attribute_value' => $product['code3']
287 );
288 }
289
290 //Päritoluriik
291 foreach($countries as $country)
292 {
293 if($product['code3'] == $country['countryID'])
294 {
295 $products['product'][$i]['attributes']['attribute'][] = array(
296
297 '@attributes' => array(
298 'type' => 'text'
299 ),
300 'attribute_name' => 'Paritoluriik',
301 'attribute_value' => $product['code3']
302 );
303 continue;
304 }
305 }
306
307
308
309 if (isset($product['parameters'])) {
310 $products['product'][$i]['attributes']['attribute'][] = array(
311
312 '@attributes' => array(
313 'type' => 'numeric'
314 ),
315 'attribute_name' => 'Korpuse moot',
316 'attribute_value' => $product['parameters'][0]['parameterValue']
317 );
318
319 $products['product'][$i]['attributes']['attribute'][] = array(
320
321 '@attributes' => array(
322 'type' => 'numeric'
323 ),
324 'attribute_name' => 'Veekindlus',
325 'attribute_value' => $product['parameters'][1]['parameterValue']
326 );
327
328 foreach($product['parameters'] as $productParameter)
329 {
330 if($productParameter['parameterGroupID'] == 6)
331 {
332 $products['product'][$i]['attributes']['attribute'][] = array(
333
334 '@attributes' => array(
335 'type' => 'numeric'
336 ),
337 'attribute_name' => 'Rihma varv',
338 'attribute_value' => $productParameter['parameterValue']
339 );
340 }
341 }
342
343 }
344
345 $i++;
346 }
347}
348
349$xml = Array2XML::createXML('products', $products);
350$myfile = fopen("products.xml", "w") or die("Unable to open file!");
351$txt = $xml->saveXML();
352fwrite($myfile, $txt);
353fclose($myfile);
354
355echo $xml->saveXML();
356
357
358
359class Array2XML
360{
361
362 private static $xml = null;
363 private static $encoding = 'UTF-8';
364
365 /**
366 * Initialize the root XML node [optional]
367 * @param $version
368 * @param $encoding
369 * @param $format_output
370 */
371 public static function init($version = '1.0', $encoding = 'UTF-8', $format_output = true)
372 {
373 self::$xml = new DomDocument($version, $encoding);
374 self::$xml->formatOutput = $format_output;
375 self::$encoding = $encoding;
376 }
377
378 /**
379 * Convert an Array to XML
380 * @param string $node_name - name of the root node to be converted
381 * @param array $arr - aray to be converterd
382 * @return DomDocument
383 */
384 public static function &createXML($node_name, $arr = array())
385 {
386 $xml = self::getXMLRoot();
387 $xml->appendChild(self::convert($node_name, $arr));
388
389 self::$xml = null; // clear the xml node in the class for 2nd time use.
390 return $xml;
391 }
392
393 /**
394 * Convert an Array to XML
395 * @param string $node_name - name of the root node to be converted
396 * @param array $arr - aray to be converterd
397 * @return DOMNode
398 */
399 private static function &convert($node_name, $arr = array())
400 {
401
402 //print_arr($node_name);
403 $xml = self::getXMLRoot();
404 $node = $xml->createElement($node_name);
405
406 if (is_array($arr)) {
407 // get the attributes first.;
408 if (isset($arr['@attributes'])) {
409 foreach ($arr['@attributes'] as $key => $value) {
410 if (!self::isValidTagName($key)) {
411 throw new Exception('[Array2XML] Illegal character in attribute name. attribute: ' . $key . ' in node: ' . $node_name);
412 }
413 $node->setAttribute($key, self::bool2str($value));
414 }
415 unset($arr['@attributes']); //remove the key from the array once done.
416 }
417
418 // check if it has a value stored in @value, if yes store the value and return
419 // else check if its directly stored as string
420 if (isset($arr['@value'])) {
421 $node->appendChild($xml->createTextNode(self::bool2str($arr['@value'])));
422 unset($arr['@value']); //remove the key from the array once done.
423 //return from recursion, as a note with value cannot have child nodes.
424 return $node;
425 } else if (isset($arr['@cdata'])) {
426 $node->appendChild($xml->createCDATASection(self::bool2str($arr['@cdata'])));
427 unset($arr['@cdata']); //remove the key from the array once done.
428 //return from recursion, as a note with cdata cannot have child nodes.
429 return $node;
430 }
431 }
432
433 //create subnodes using recursion
434 if (is_array($arr)) {
435 // recurse to get the node for that key
436 foreach ($arr as $key => $value) {
437 if (!self::isValidTagName($key)) {
438 throw new Exception('[Array2XML] Illegal character in tag name. tag: ' . $key . ' in node: ' . $node_name);
439 }
440 if (is_array($value) && is_numeric(key($value))) {
441 // MORE THAN ONE NODE OF ITS KIND;
442 // if the new array is numeric index, means it is array of nodes of the same kind
443 // it should follow the parent key name
444 foreach ($value as $k => $v) {
445 $node->appendChild(self::convert($key, $v));
446 }
447 } else {
448 // ONLY ONE NODE OF ITS KIND
449 $node->appendChild(self::convert($key, $value));
450 }
451 unset($arr[$key]); //remove the key from the array once done.
452 }
453 }
454
455 // after we are done with all the keys in the array (if it is one)
456 // we check if it has any text value, if yes, append it.
457 if (!is_array($arr)) {
458 $node->appendChild($xml->createTextNode(self::bool2str($arr)));
459 }
460
461 return $node;
462 }
463
464 /*
465 * Get the root XML node, if there isn't one, create it.
466 */
467 private static function getXMLRoot()
468 {
469 if (empty(self::$xml)) {
470 self::init();
471 }
472 return self::$xml;
473 }
474
475 /*
476 * Get string representation of boolean value
477 */
478 private static function bool2str($v)
479 {
480 //convert boolean to text value.
481 $v = $v === true ? 'true' : $v;
482 $v = $v === false ? 'false' : $v;
483 return $v;
484 }
485
486 /*
487 * Check if the tag name or attribute name contains illegal characters
488 * Ref: http://www.w3.org/TR/xml/#sec-common-syn
489 */
490 private static function isValidTagName($tag)
491 {
492 $pattern = '/^[a-z_]+[a-z0-9\:\-\.\_]*[^:]*$/i';
493 return preg_match($pattern, $tag, $matches) && $matches[0] == $tag;
494 }
495
496 /*
497 * Convert xml string into array.
498 */
499 public static function XML_TO_ARR($xmlstring)
500 {
501 $xml = simplexml_load_string($xmlstring, "SimpleXMLElement", LIBXML_NOCDATA);
502 $json = json_encode($xml);
503 $array = json_decode($json, TRUE);
504 return $array;
505 }
506}
507
508?>