Bypass cors restrictions in javascript with PHP

Problem

Modern Browsers don't allow you to send requests to another website that doesn't have the Cross-Origin Header set.

My specific problem:
I wanted to write a browser-based viewer of someone's REST API, but I couldn't get them via a Javascript request, only with non-browser requests.

Solution

PHP doesn't care about Cross Origin requests and is easy to setup.

Create a small "PHP proxy" to get the resource and forward it to your browser's javascript request.

Example PHP code (filename: t.php):

<?php
$json = file_get_contents('https://example.com/api/v1/status?key=XXX&server='.$_GET['id']);
$arr = json_decode($json, true); //Accessible Array from JSON answer
echo $arr['response']['serverstatus'];
?>

Now simply make an ajax call from javascript and access this resource over the PHP file.
Javascript example:
return $.ajax({
  type: "GET",
  url: "https://myserver.com/t.php?id="+id
}).done(function(data) {
  document.body.innerHTML += data+"<br>";
});

This is a quick and easy fix for testing, but should be fledged out for production usage. It is missing rate-limiting and caching on the PHP side.
With a rate-limit you would refrain from spamming the target API and secure your own service from spam too.