An Option for dealing with CORS

Small red plastic toy shovel.

As we do more things with JSON, I run into CORS access issues. They can be solved in various ways but it’s often a hassle. It often irritates me as I build demos in COde

After wanting a simple solution this AM, I made this tiny, insignificant PHP file that I think might be useful to others.

Name this file cors.php.1

Put this file on a server.

You can now append some JSON URL to that URL like so … https://yourserver.com/cors.php?url=https://someurl.com/data.json and it returns drama-free JSON for your use and enjoyment. Assuming you have HTTPs on your server it should also deal with HTTP/HTTPS conflicts as well which is very handy at times.

<?php 
header("Access-Control-Allow-Origin: *");//let the people in
header("content-type: application/json");//make the return all JSON-Y
$url = htmlspecialchars($_GET["url"]);//get the URL parameter from the . . . URL
echo file_get_contents($url);//spit it back out


1 Or name it whatever you want. I’m going using bossiness as a shortcut to clarity.