<?php
/**
* Compresses JSON payloads for routine automation with verbose logging.
*
* @param string $jsonPayload The JSON payload to compress.
* @return string|false The compressed JSON payload, or false on error.
*/
function compressJsonPayload(string $jsonPayload): string|false
{
// Verbose logging - log the input payload
error_log("Input JSON Payload:\n" . $jsonPayload);
try {
// Decode the JSON payload
$data = json_decode($jsonPayload, true);
if ($data === null) {
error_log("Error decoding JSON: " . json_last_error_msg());
return false; // Return false on decoding error
}
// Compress the decoded data. Using gzencode for compression
$compressedData = gzencode(json_encode($data), 9, GZ_COMPRESSION_DEFLATE);
// Verbose logging - log the compressed data
error_log("Compressed JSON Payload:\n" . $compressedData);
return $compressedData;
} catch (Exception $e) {
error_log("An unexpected error occurred: " . $e->getMessage());
return false; // Return false on unexpected errors
}
}
// Example usage (for testing)
/*
$json = '{
"name": "John Doe",
"age": 30,
"city": "New York",
"details": {
"occupation": "Software Engineer",
"skills": ["PHP", "JavaScript", "MySQL"]
}
}';
$compressed = compressJsonPayload($json);
if ($compressed !== false) {
echo "Compression successful.\n";
// You can now use the $compressed variable for automation
// For example, sending it to an API.
} else {
echo "Compression failed.\n";
}
*/
?>
Add your comment