format('YmdH00'); $dateTimeEnd = $dateTimeStart->modify('+23 hours'); $periodEnd = $dateTimeEnd->format('YmdH00'); } catch (Exception $e) { echo "Error creating date objects: " . $e->getMessage() . "\n"; return false; } $apiUrl = API_BASE_URL . '?' . http_build_query([ 'securityToken' => SECURITY_TOKEN, 'documentType' => 'A44', // Price Document 'in_Domain' => IN_OUT_DOMAIN, 'out_Domain' => IN_OUT_DOMAIN, 'periodStart' => $periodStart, 'periodEnd' => $periodEnd, ]); echo "Fetching raw XML data from ENTSO-E API for $day...\n"; // echo "API URL: $apiUrl\n"; // Uncomment for debugging // Initialize cURL $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $apiUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, false); $xmlResponse = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpCode !== 200 || !$xmlResponse) { echo "Error: HTTP Code $httpCode or failed to get response.\n"; return false; } // Check for Acknowledge document (error/no data) if (str_contains($xmlResponse, '')) { echo "Error: The API returned an Acknowledge document (often indicating missing data or an error).\n"; echo "Check the XML content for details before proceeding.\n"; } // Save to XML file $filename = "prices_{$day}.xml"; if (file_put_contents($filename, $xmlResponse) === false) { echo "Error: Could not save data to file: $filename\n"; return false; } echo "Successfully saved raw XML response to **$filename**.\n"; return $filename; } // --- Main Execution --- if ($argc < 2) { echo "Usage: php Collector.php YYYYMMDD\n"; exit(1); } $targetDay = $argv[1]; if (!preg_match('/^\d{8}$/', $targetDay)) { echo "Invalid date format. Please use YYYYMMDD (e.g., 20240101).\n"; exit(1); } downloadAndSaveRawXml($targetDay); ?>