' . $apiKey . ' '; $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $request); curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: application/xml"]); $response = curl_exec($ch); if (curl_errno($ch)) { error_log("CURL error: " . curl_error($ch)); curl_close($ch); return null; } curl_close($ch); $xml = simplexml_load_string($response); if ($xml === false) { error_log("Failed to parse Unas response XML."); return null; } return (string)$xml->Token; } // Malfini API autentikáció function authenticateMalfini($username, $password) { $url = "https://api.malfini.com/api/v4/api-auth/login"; $data = [ 'username' => $username, 'password' => $password ]; $options = [ 'http' => [ 'header' => "Content-Type: application/json\r\n", 'method' => 'POST', 'content' => json_encode($data), ], ]; $context = stream_context_create($options); $result = file_get_contents($url, false, $context); if ($result === FALSE) { error_log("Authentication failed!"); return null; } $response = json_decode($result, true); return $response['access_token'] ?? null; } // Malfini termékek lekérése function getProducts($accessToken) { $url = "https://api.malfini.com/api/v4/product"; $options = [ 'http' => [ 'header' => "Authorization: Bearer $accessToken\r\n", 'method' => 'GET', ], ]; $context = stream_context_create($options); $result = file_get_contents($url, false, $context); if ($result === FALSE) { error_log("Failed to fetch products!"); return []; } $response = json_decode($result, true); return $response; } // XML generálása az Unas API-hoz function buildUnasXml($products) { $xml = new SimpleXMLElement(''); foreach ($products as $product) { $item = $xml->addChild('Product'); $item->addChild('Sku', htmlspecialchars($product['code'])); $item->addChild('Name', htmlspecialchars($product['name'])); $item->addChild('Category', htmlspecialchars($product['categoryName'])); $item->addChild('Description', htmlspecialchars($product['description'])); } return $xml->asXML(); } // Unas termékek frissítése function updateUnasProducts($token, $products) { $url = "https://api.unas.eu/shop/?action=setProduct"; $xmlData = buildUnasXml($products); $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlData); curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Authorization: Bearer $token", "Content-Type: application/xml" ]); $response = curl_exec($ch); if (curl_errno($ch)) { error_log("CURL error: " . curl_error($ch)); curl_close($ch); return null; } curl_close($ch); error_log("Unas API response:"); error_log($response); return $response; } // Szinkronizáció futtatása function runSync() { $unasApiKey = "75a6bbc7303bbef4c38ba9ed13f95ace39c07f4f"; $malfiniUsername = "info@zsona.hu"; $malfiniPassword = "Ex112233"; $unasToken = authenticateUnas($unasApiKey); if (!$unasToken) { error_log("Failed to authenticate with Unas!"); return; } $accessToken = authenticateMalfini($malfiniUsername, $malfiniPassword); if (!$accessToken) { error_log("Failed to authenticate with Malfini!"); return; } $products = getProducts($accessToken); $response = updateUnasProducts($unasToken, $products); if ($response) { error_log("Products updated successfully."); } else { error_log("Failed to update products in Unas!"); } } // Futtasd a szinkronizációt runSync();