· 5 years ago · Jan 21, 2021, 10:20 AM
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 $client = new SoapClient("https://api.amica.com.pl/?q=ws/wsdl/products", array());
40
41 if($langCode == 'ee') $langCode = 'et'; // Estonia
42 if($langCode == 'rs') $langCode = 'sr'; // Serbia
43 if($langCode == 'kz') $langCode = 'kk'; // Kazachstan
44
45 $response = $client->prodsProductsGet($catCode, '', '', $filter, '', '', 200, '1', $langCode, 0);
46
47 if(!isset($response->dataResponse->productsDataList->productData)){
48 echo json_encode([]);
49 exit();
50 }
51 $productsResp = $response->dataResponse->productsDataList->productData;
52
53 /**
54 * IF SINGLE PRODUCT RESPONSE DOES NO RETURN ARRAY, ONLY SINGLE OBJECT
55 */
56 if(isset($productsResp->prodID)){
57 echo json_encode(array($productsResp->prodID));
58 exit();
59 }
60
61 $productsIds = array();
62 foreach($productsResp as $key => $prod){
63 // if($prod->prodID == null) continue;
64 $productsIds[] = $prod->prodID;
65 }
66
67 echo json_encode($productsIds);
68 exit();
69 }
70
71 /**
72 * PREPARE FILTER TABLE TO API QUERY
73 */
74 public function filter_prepareFilter()
75 {
76 # CHECK FILTER IS ARRAY
77 $filter = json_decode($_POST['filterArray']);
78 if(!is_array($filter)) return '';
79 if(count($filter) <= 0) return '';
80
81 # PREPARE FILTER ARRAY
82 $filterArr = [];
83 $lp = 0;
84 foreach($filter as $key => $fArr){
85 if(count($fArr) !== 2) return '';
86 $filterArr[$lp]['attrCatID'] = $fArr[0];
87 $filterArr[$lp]['attrID'] = $fArr[1];
88 $lp++;
89 }
90
91 return $filterArr;
92 }
93
94
95 # REDIRED & PAGE SECTION
96 #
97 public function redirect301()
98 {
99 header("HTTP/1.1 301 Moved Permanently");
100 header("location: " . $this->urlBase());
101 exit();
102 }
103
104 public function page403()
105 {
106 header('HTTP/1.0 403 Forbidden');
107 exit('You are not allowed to access this file. Check <a href="'.$_SERVER['HTTP_REFERER'].'">homepage</a> for more information.');
108 }
109
110
111 # URL SECTION
112 #
113 public function urlBase()
114 {
115 return $this->urlProtocol() . '://' . $_SERVER['SERVER_NAME'];
116 //$this->urlProtocol() . '://' . $_SERVER['SERVER_NAME'];
117 exit();
118 }
119
120 public function urlProtocol()
121 {
122 $protocol = 'http';
123 if(isset($_SERVER['HTTPS']))
124 {
125 if($_SERVER['HTTPS'] == 'on')
126 {
127 $protocol = 'https';
128 }
129 }
130
131 return $protocol;
132 }
133
134 # UTILITIES
135 #
136 public function prt($arr)
137 {
138 echo '<pre>';
139 print_r($arr);
140 echo '</pre>';
141 }
142
143}
144
145new api();
146