1. <?php
  2. /**
  3. * Synchronously replaces values in HTTP response for a one-off script.
  4. * This is intended for a single execution and not for persistent server-side modification.
  5. *
  6. * @param string $url The URL to fetch the response from.
  7. * @param array $replacements An associative array where keys are the values to replace
  8. * in the response and values are the replacement values.
  9. * @return string|false The modified response body on success, or false on failure.
  10. */
  11. function synchronousResponseReplacement(string $url, array $replacements): string|false
  12. {
  13. // Fetch the response content
  14. $response = file_get_contents($url);
  15. if ($response === false) {
  16. error_log("Error fetching URL: $url");
  17. return false; // Indicate failure
  18. }
  19. // Perform the replacements
  20. foreach ($replacements as $search => $replace) {
  21. $response = str_replace($search, $replace, $response);
  22. }
  23. return $response;
  24. }
  25. // Example usage:
  26. //$url = 'https://example.com';// Replace with your target URL.
  27. //$replacements = ['old_value' => 'new_value', 'another_old' => 'another_new'];
  28. //$modifiedResponse = synchronousResponseReplacement($url, $replacements);
  29. //if ($modifiedResponse !== false) {
  30. // echo $modifiedResponse; // Output the modified response
  31. //} else {
  32. // echo "Failed to modify response.";
  33. //}
  34. ?>

Add your comment