title: Add Access-Control-Allow-Origin to header in PHP
body: <p>I am trying to workaround CORS restriction on a WebGL application. I have a Web Service which resolves URL and returns images. Since this web service is not CORS enabled, I can't use the returned images as textures.</p>
  
  <p>I was planning to:</p>
  
  <ol>
  <li>Write a PHP script to handle image requests</li>
  <li>Image requests would be sent through the query string as a url
  parameter</li>
  </ol>
  
  <p>The PHP Script will:</p>
  
  <ol>
  <li>Call the web service with the query string url</li>
  <li>Fetch the image response (web service returns a content-type:image response)</li>
  <li>Add the CORS header (Add Access-Control-Allow-Origin) to the
  response</li>
  <li>Send the response to the browser</li>
  </ol>
  
  <p>I tried to implement this using a variety of techniques including CURL, HTTPResponse, plain var_dump etc. but got stuck at some point in each.</p>
  
  <p>So I have 2 questions:</p>
  
  <ol>
  <li>Is the approach good enough?</li>
  <li>Considering the approach is good enough:</li>
  </ol>
  
  <p>I made the most progress with CURL. I could get the image header and data with:</p>
  
  <pre><code>$ch = curl_init();
  $url = $_GET["url"];
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_HEADER, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:image/jpeg'));
  
  //Execute request 
      $response = curl_exec($ch);
  
  //get the default response headers 
      $headers = curl_getinfo($ch);
  
  //close connection 
      curl_close($ch);
  </code></pre>
  
  <p>But this doesn't actually change set the response content-type to image/jpeg. It dumps the header + response into a new response of content-type text/html and display the header and the image BLOB data in the browser.</p>
  
  <p>How do I get it to send the response in the format I want?</p>