【问题标题】:Google Cloud PHP CORS issue谷歌云 PHP CORS 问题
【发布时间】:2021-09-02 03:18:14
【问题描述】:

我已使用 PHP(测试版)语言将 PHP api 迁移到谷歌云功能。代码从外部应用程序请求数据,这些数据以 XML 格式返回,然后 API 获取结果并将其输出为 JSON 以供 Angular 应用程序读取

我的代码在使用 POSTMAN 时有效(所以我知道它返回正确的数据)但是当从本地或托管在 google firebase 上的 Angular 应用程序运行时,我得到以下 CORS 问题

Request URL: [[google firebase cloud function url]]
Request Method: OPTIONS
Status Code: 500
Referrer Policy: strict-origin-when-cross-origin
Provisional headers are shown
Accept: application/json, text/plain, */*
Content-Type: application/moca-xml
Referrer: http://localhost:4200/
Response-Encoder: application/xml
Response-Type: text
Request Payload:
[[Data sent to external app to request data ]]

使用的原始 PHP API 通过设置标头绕过 CORS:

//set header of return output
header('Access-Control-Allow-Origin: *'); 
header("Access-Control-Allow-Credentials: true");
header('Access-Control-Allow-Methods: POST');
header('Access-Control-Max-Age: 1000');
header('Access-Control-Allow-Headers: Origin, Content-Type, Response-Encoder, Response-Type');  
header('Content-Type: application/json');

在google firebase cloud函数中,无论是使用上面还是下面建议的方法都没有效果,导致CORS错误

$headers = ['Access-Control-Allow-Origin' => '*'];
$headers = array_merge($headers, [
            'Access-Control-Allow-Methods' => 'POST, OPTIONS',
            'Access-Control-Allow-Credentials' => 'true',
            'Access-Control-Allow-Headers' => 'Origin, Content-Type, Response-Encoder, Response-Type',
            'Access-Control-Max-Age' => '1000',
            'Content-Type' => 'application/json'            
        ]);

.... code to get data ....

return new Response(200, $headers, $returndata);


【问题讨论】:

  • Status Code: 500 - 因此您尝试使用状态码 200 加上 CORS 标头进行响应,显然没有奏效。所以去找出导致 500 的原因,首先检查相关的错误日志。
  • 而且仅供参考,OPTIONS 请求当然不应该通过发送 data 来回答。
  • 是的,OPTIONS 上的返回数据是一个问题。从控制台我可以看到我有两个 CORS 问题并且未在 OPTIONS 上发送数据已通过以下使用 if ($request->getMethod() === 'OPTIONS') { $headers = array_merge($headers, [ 'Access-Control-Allow-Methods' => 'GET', 'Access-Control-Allow-Headers' => 'Content-Type', 'Access-Control-Max-Age' => '3600' ]); return new Response(200, $headers, ''); } 修复
  • 我在谷歌云控制台或 Firebase 控制台的云功能上找不到任何像样的日志,但是 CORS 现在说引用者:localhost:4200 response-encoder: application/xml Accept: application/ json,文本/纯文本,/
  • API 通过 CURL 连接到外部应用程序,因此我尝试设置这些标头,即使引用者是我的本地主机,我认为它是调用外部应用程序的云函数,这给了我问题. curl_setopt ($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/moca-xml","Response-Encoder:application/xml", "Access-Control-Allow-Origin: *", "Access-Control-Allow-Methods: POST, GET", "Access-Control-Allow-Headers: Origin, Content-Type, Response-Encoder, Response-Type"));

标签: php google-cloud-functions cors


【解决方案1】:

首先,调用谷歌云PHP函数的角度应用程序不允许某些标题,最后我的标题只是

private headers = new HttpHeaders({'Content-Type':'application/json'});

然后在 google cloud PHP 函数中,OPTIONS 请求必须通过返回 200 或 204 以及在响应中设置标头来处理

 $headers = ['Access-Control-Allow-Origin' => '*'];
 if ($request->getMethod() === 'OPTIONS') {
     $headers = array_merge($headers, [
            'Access-Control-Allow-Methods' => 'GET',
            'Access-Control-Allow-Headers' => 'Content-Type',
            'Access-Control-Max-Age' => '3600'          
        ]);
    return new Response(200, $headers, '');
 } else {
     $headers = array_merge($headers, [
                'Access-Control-Allow-Methods' => 'POST',
                'Access-Control-Allow-Credentials' => 'true',
                'Access-Control-Allow-Headers' => 'Origin, Content-Type',
                'Access-Control-Max-Age' => '1000', 
                'Content-Type' => 'application/json'    
            ]);
 }

【讨论】:

    猜你喜欢
    • 2022-01-20
    • 2017-03-30
    • 1970-01-01
    • 2017-03-02
    • 1970-01-01
    • 2020-11-21
    • 2015-05-09
    • 2017-10-06
    • 2020-09-25
    相关资源
    最近更新 更多