ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// API configuration
$api_url_products = 'https://oonela.com/catalogs/api/10035/CAT-1LJNXZPPRO/products';
$api_url_info = 'https://oonela.com/catalogs/api/10035/CAT-1LJNXZPPRO/categories';
$username = 'OONCAT-10035-3ICRW-G2Y9Z-2MMGW';
$password = '1LJNXZPPRO';
// API fetch function with enhanced security
function fetch_api_data($url, $username, $password) {
// Generate security headers
$timestamp = time();
$message = $timestamp . $username;
$signature = hash_hmac('sha256', $message, $username);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true); // Use POST method
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// Add security headers
$headers = [
'X-Timestamp: ' . $timestamp,
'X-API-Security: ' . $signature
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($response === false) {
die('API URL is incorrect or network issue: ' . curl_error($ch));
} elseif ($http_status == 401) {
die('Authentication failed: Username or password is incorrect, or security headers are invalid');
} elseif ($http_status == 404) {
die('API endpoint not found: Invalid company ID, catalog module not activated, or missing API settings');
} elseif ($http_status != 200) {
die('API error: HTTP status ' . $http_status);
}
curl_close($ch);
return $response;
}
// Fetch products and categories info from API
$product_response = fetch_api_data($api_url_products, $username, $password);
$info_response = fetch_api_data($api_url_info, $username, $password);
// Decode the JSON responses
$product_data = json_decode($product_response, true);
$info_data = json_decode($info_response, true);
// Check if the product data or info data is empty
if (empty($product_data) || empty($info_data)) {
die('No products or info found.');
}
// Extract product groups and additional groups for categories and brands
$product_groups = $info_data['product_groups'] ?? [];
$product_groups2 = isset($info_data['product_additional_groups'][1]) ? $info_data['product_additional_groups'][1] : [];
// Extract unique categories from the product data
$categories = $product_groups;
asort($categories); // Sort categories by name in ascending order
// Extract unique brands from the product data
$brands = $product_groups2;
asort($brands); // Sort brands by name in ascending order
// Filter products by category or brand if a category or brand is selected
$selected_category = $_GET['category'] ?? '';
$selected_brand = $_GET['merk'] ?? '';
$search_query = $_GET['search'] ?? '';
// Get the number of items per page
$limit = (int)($_GET['limit'] ?? 12); // Default items per page is 12
// Filter products based on selected category, brand, and search query
$filtered_products = array_filter($product_data, function ($product) use ($selected_category, $selected_brand, $product_groups, $product_groups2, $search_query) {
$category_code = $product['cod_fam_prd_path'];
$brand_code = $product['cod_grp_prd_path_1'];
$category_name = $product_groups[$category_code] ?? '';
$brand_name = $product_groups2[$brand_code] ?? '';
// Match exact category or subcategories (e.g., "JOGHURT" matches "JOGHURT / NATUREL")
$matches_category = !$selected_category || $category_name === $selected_category || strpos($category_name, $selected_category . ' / ') === 0;
$matches_brand = !$selected_brand || $brand_name === $selected_brand;
$matches_search = !$search_query || stripos($product['lib_prd'], $search_query) !== false;
return $matches_category && $matches_brand && $matches_search;
});
// Pagination logic
$page = (int)($_GET['page'] ?? 1);
$offset = ($page - 1) * $limit;
$paginated_products = array_slice($filtered_products, $offset, $limit);
$total_results = count($filtered_products);
$total_pages = ceil($total_results / $limit);
?>