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