将 Access-Control-Allow-Origin 添加到 PHP 中的标头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12630589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:55:52  来源:igfitidea点击:

Add Access-Control-Allow-Origin to header in PHP

phpcurlhttp-headerscors

提问by SANDeveloper

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.

我正在尝试解决 WebGL 应用程序上的 CORS 限制。我有一个解析 URL 并返回图像的 Web 服务。由于此 Web 服务未启用 CORS,因此我无法将返回的图像用作纹理。

I was planning to:

我打算:

  1. Write a PHP script to handle image requests
  2. Image requests would be sent through the query string as a url parameter
  1. 编写一个PHP脚本来处理图片请求
  2. 图像请求将通过查询字符串作为 url 参数发送

The PHP Script will:

PHP 脚本将:

  1. Call the web service with the query string url
  2. Fetch the image response (web service returns a content-type:image response)
  3. Add the CORS header (Add Access-Control-Allow-Origin) to the response
  4. Send the response to the browser
  1. 使用查询字符串 url 调用 Web 服务
  2. 获取图像响应(Web 服务返回 content-type:image 响应)
  3. 将 CORS 标头(Add Access-Control-Allow-Origin)添加到响应中
  4. 将响应发送到浏览器

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.

我尝试使用各种技术(包括 CURL、HTTPResponse、plain var_dump 等)来实现这一点,但在每个技术的某个点上都卡住了。

So I have 2 questions:

所以我有两个问题:

  1. Is the approach good enough?
  2. Considering the approach is good enough:
  1. 方法是否足够好?
  2. 考虑到这种方法已经足够好了:

I made the most progress with CURL. I could get the image header and data with:

我在 CURL 方面取得了最大的进步。我可以通过以下方式获取图像标题和数据:

$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.

但这实际上并没有改变将响应内容类型设置为图像/jpeg。它将标头 + 响应转储为内容类型 text/html 的新响应,并在浏览器中显示标头和图像 BLOB 数据。

How do I get it to send the response in the format I want?

如何让它以我想要的格式发送响应?

回答by SANDeveloper

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();

回答by Lukas Liesis

make sure that Apache (if you are using Apache) has mod_headersloaded before using all that stuff with headers.

在使用所有带有标题的东西之前,请确保 Apache(如果您使用的是 Apache)已加载mod_headers

(following tips works on Ubuntu, don't know about other distributions)

(以下提示适用于 Ubuntu,不知道其他发行版)

you can check list of loaded modules with

你可以检查加载的模块列表

apache2ctl -M

to enable mod_headers you can use

启用 mod_headers 你可以使用

a2enmod headers

of course after any changes in Apache you have to restart it:

当然,在 Apache 中进行任何更改后,您必须重新启动它:

/etc/init.d/apache2 restart

after this try adding this line to your .htaccess, or of course use php headers

在此之后尝试将此行添加到您的 .htaccess,或者当然使用 php 标头

<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
</IfModule>

PHP:

PHP:

header("Access-Control-Allow-Origin: *");