Skip to content

Instantly share code, notes, and snippets.

@vivithemage
Last active September 18, 2018 11:02
Show Gist options
  • Save vivithemage/09807738bd98060f9bdace9a910e1561 to your computer and use it in GitHub Desktop.
Save vivithemage/09807738bd98060f9bdace9a910e1561 to your computer and use it in GitHub Desktop.
Resource proxy with search and replace functionaltiy
<?php
/*
Resource proxy with search and replace functionality on resources (e.g. css, js) in order to get around CORS restrictions.
https://hubbounce.services.example.co.uk/?resource_url=https://example.com/wp-content/themes/example-training/dist/css/main.min.css&amp;search=url(../fonts/&amp;replace=url(https://cdn2.hubspot.net/hubfs/4668366/fonts/
*/
$resource_url = $_GET['resource_url'];
$search = $_GET['search'];
$replace = $_GET['replace'];
$returned_data = '';
// check if url before
if(filter_var($resource_url, FILTER_VALIDATE_URL) === FALSE) {
header("HTTP/1.0 404 Not Found");
echo "Not found.\n";
echo $resource_url;
die();
} else {
$returned_data = file_get_contents($resource_url);
header("Content-type: text/css; charset: UTF-8");
echo str_replace($search, $replace, $returned_data);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment