· 5 years ago · Jan 27, 2021, 05:06 PM
1<?php
2
3# FILE: hansa/api/index.php
4
5class api{
6
7 public function __construct(){
8 $this->init();
9 }
10
11
12 public function init()
13 {
14 if($_SERVER['REQUEST_METHOD'] == 'POST')
15 {
16 if(isset($_POST['filter_ajaxGetProductsIds'])) $this->filter_ajaxGetProductsIds();
17 }
18
19 $this->redirect301();
20 }
21
22 public function filter_ajaxGetProductsIds()
23 {
24 # PREPARE FILTER TABLE TO API QUERY
25 if(!isset($_POST['filterArray'])) exit(0);
26 $filter = $this->filter_prepareFilter();
27
28 if(!isset($_POST['catCode'])) exit(0);
29 $catCode = $_POST['catCode'];
30
31 if(!isset($_POST['langCode'])) exit(0);
32 $langCode = $_POST['langCode'];
33
34 $products = $this->filter_getApiProductsIDs($catCode,$filter,$langCode);
35 }
36
37 public function filter_getApiProductsIDs($catCode,$filter,$langCode)
38 {
39 # EXECUTION TIME COUNT - START
40 $start = microtime(true);
41
42 $client = new SoapClient("https://api.amica.com.pl/?q=ws/wsdl/products", array());
43
44 if($langCode == 'ee') $langCode = 'et'; // Estonia
45 if($langCode == 'rs') $langCode = 'sr'; // Serbia
46 if($langCode == 'kz') $langCode = 'kk'; // Kazachstan
47
48 $response = $client->prodsProductsGet($catCode, '', '', $filter, '', '', 200, '1', $langCode, 0);
49
50 # EXECUTION TIME COUNT - END
51 $time_elapsed_secs = microtime(true) - $start;
52
53 if(!isset($response->dataResponse->productsDataList->productData)){
54 echo json_encode([]);
55 exit();
56 }
57 $productsResp = $response->dataResponse->productsDataList->productData;
58
59
60 /**
61 * IF SINGLE PRODUCT RESPONSE DOES NO RETURN ARRAY, ONLY SINGLE OBJECT
62 */
63 if(isset($productsResp->prodID)){
64 echo json_encode(array($productsResp->prodID));
65 exit();
66 }
67
68 $productsIds = array();
69 foreach($productsResp as $key => $prod){
70 $productsIds[] = $prod->prodID;
71 }
72
73 echo json_encode([$time_elapsed_secs,$productsIds]);
74 exit();
75 }
76
77 /**
78 * PREPARE FILTER TABLE TO API QUERY
79 */
80 public function filter_prepareFilter()
81 {
82 # CHECK FILTER IS ARRAY
83 $filter = json_decode($_POST['filterArray']);
84 if(!is_array($filter)) return '';
85 if(count($filter) <= 0) return '';
86
87 # PREPARE FILTER ARRAY
88 $filterArr = [];
89 $lp = 0;
90 foreach($filter as $key => $fArr){
91 if(count($fArr) !== 2) return '';
92 $filterArr[$lp]['attrCatID'] = $fArr[0];
93 $filterArr[$lp]['attrID'] = $fArr[1];
94 $lp++;
95 }
96
97 return $filterArr;
98 }
99
100
101 # REDIRED & PAGE SECTION
102 #
103 public function redirect301()
104 {
105 header("HTTP/1.1 301 Moved Permanently");
106 header("location: " . $this->urlBase());
107 exit();
108 }
109
110 public function page403()
111 {
112 header('HTTP/1.0 403 Forbidden');
113 exit('You are not allowed to access this file. Check <a href="'.$_SERVER['HTTP_REFERER'].'">homepage</a> for more information.');
114 }
115
116
117 # URL SECTION
118 #
119 public function urlBase()
120 {
121 return $this->urlProtocol() . '://' . $_SERVER['SERVER_NAME'];
122 exit();
123 }
124
125 public function urlProtocol()
126 {
127 $protocol = 'http';
128 if(isset($_SERVER['HTTPS']))
129 {
130 if($_SERVER['HTTPS'] == 'on')
131 {
132 $protocol = 'https';
133 }
134 }
135
136 return $protocol;
137 }
138
139 # UTILITIES
140 #
141 public function prt($arr)
142 {
143 echo '<pre>';
144 print_r($arr);
145 echo '</pre>';
146 }
147
148}
149
150new api();
151