<?php
/**
* Synchronously replaces values in HTTP response for a one-off script.
* This is intended for a single execution and not for persistent server-side modification.
*
* @param string $url The URL to fetch the response from.
* @param array $replacements An associative array where keys are the values to replace
* in the response and values are the replacement values.
* @return string|false The modified response body on success, or false on failure.
*/
function synchronousResponseReplacement(string $url, array $replacements): string|false
{
// Fetch the response content
$response = file_get_contents($url);
if ($response === false) {
error_log("Error fetching URL: $url");
return false; // Indicate failure
}
// Perform the replacements
foreach ($replacements as $search => $replace) {
$response = str_replace($search, $replace, $response);
}
return $response;
}
// Example usage:
//$url = 'https://example.com';// Replace with your target URL.
//$replacements = ['old_value' => 'new_value', 'another_old' => 'another_new'];
//$modifiedResponse = synchronousResponseReplacement($url, $replacements);
//if ($modifiedResponse !== false) {
// echo $modifiedResponse; // Output the modified response
//} else {
// echo "Failed to modify response.";
//}
?>
Add your comment