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.
I was planning to:
The PHP Script will:
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.
So I have 2 questions:
I made the most progress with CURL. I could get the image header and data with:
$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);
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.
How do I get it to send the response in the format I want?
You can add the origin of the request to the list of domains authorized to access the server's resources by adding it to the values of the Access-Control-Allow-Origin header. You can set it via the header() function of PHP (https://www.php.net/manual/fr/function.header.php).
What is the Access-Control-Allow-Origin response header? The Access-Control-Allow-Origin header is included in the response from one website to a request originating from another website, and identifies the permitted origin of the request.
Cross-origin resource sharing (CORS) is a technique that allow servers to serve resources to permitted origin domains by adding HTTP headers to the server who are respected from web browsers. Examples of practical use of CORS are cross-domain AJAX requests, or using fonts hosted on a subdomain.
The simplest approach turned out to be the answer. Just had to insert the header before sending the response off.
$ch = curl_init();
$url = $_GET["url"];
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
//Execute request
$response = curl_exec($ch);
//get the default response headers
header('Content-Type: image/jpeg');
header("Access-Control-Allow-Origin: *");
//close connection
curl_close($ch);
flush();
make sure that Apache (if you are using Apache) has mod_headers loaded before using all that stuff with headers.
(following tips works on Ubuntu, don't know about other distributions)
you can check list of loaded modules with
apache2ctl -M
to enable mod_headers you can use
a2enmod headers
of course after any changes in Apache you have to restart it:
/etc/init.d/apache2 restart
after this try adding this line to your .htaccess, or of course use php headers
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
PHP:
header("Access-Control-Allow-Origin: *");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With