<?php
/**
* Serializes API response objects for temporary storage with graceful failure handling.
*
* @param mixed $obj The object to serialize.
* @param string $filename The filename to store the serialized data.
* @return bool True on success, false on failure.
*/
function serializeApiResponse(mixed $obj, string $filename): bool
{
try {
// Serialize the object.
$serializedData = serialize($obj);
// Write the serialized data to the file.
$result = file_put_contents($filename, $serializedData);
// Check if the file write was successful.
if ($result === false) {
// Handle file write failure.
error_log("Failed to write to file: " . $filename);
return false;
}
return true;
} catch (Exception $e) {
// Handle any other exceptions.
error_log("Serialization or file write error: " . $e->getMessage());
return false;
}
}
Add your comment