简体   繁体   English

谷歌云 PHP CORS 问题

[英]Google Cloud PHP CORS issue

I have migrated a PHP api to google cloud functions using the PHP (beta) language.我已使用 PHP(测试版)语言将 PHP api 迁移到谷歌云函数。 The code requests data from an external application which is returned in an XML format and then the API gets the results and outputs them as JSON for the angular app to read代码从外部应用程序请求数据,该数据以 XML 格式返回,然后 API 获取结果并将其输出为 JSON 以供 angular 应用程序读取

My code works when using POSTMAN (so I know it returns the correct data) but when running from my Angular app either locally or hosted on google firebase I get the below CORS issue我的代码在使用 POSTMAN 时有效(所以我知道它返回正确的数据)但是当从我的 Angular 应用程序在本地或托管在 google firebase 上运行时,我得到以下 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 ]]

The original PHP API used got around CORS by setting headers:使用的原始 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');

In the google firebase cloud function, both using the above or the suggested below method have no effect and result in the CORS errors在 google firebase 云函数中,无论使用上述方法还是下面建议的方法都没有效果并导致 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);


Firstly the angular app calling the google cloud PHP function was not allowed certain headers and in the end my header was just首先,调用 google cloud PHP 函数的 angular 应用程序不允许某些标题,最后我的标题只是

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

Then in the google cloud PHP function, the OPTIONS request has to be handled by returning 200 or 204 as well as setting the headers on the response然后在谷歌云 PHP 函数中,必须通过返回 200 或 204 以及在响应上设置标头来处理 OPTIONS 请求

 $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'    
            ]);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM