Cors 如何在PHP中为amp表单输出正确的标题?

Cors 如何在PHP中为amp表单输出正确的标题?,cors,amp-html,Cors,Amp Html,有关amp表单的CORS标题的文档可能会更容易,我仍然有点不知道我是否做对了每件事 现在,我的表格似乎可以从我自己的网站以及谷歌的AMP结果中使用。不过,在我的开发网站上它不起作用;我也不确定它是否真的很安全。这是我目前正在使用的代码,用于一个脚本 这是大量尝试和错误的结果,我忍不住想,关于这个问题的文档可能会更清晰 header('Cache-Control: private, no-cache'); header('Access-Control-Allow-Origin: '.$_SERVE

有关amp表单的CORS标题的文档可能会更容易,我仍然有点不知道我是否做对了每件事

现在,我的表格似乎可以从我自己的网站以及谷歌的AMP结果中使用。不过,在我的开发网站上它不起作用;我也不确定它是否真的很安全。这是我目前正在使用的代码,用于一个脚本

这是大量尝试和错误的结果,我忍不住想,关于这个问题的文档可能会更清晰

header('Cache-Control: private, no-cache');
header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN']);
header('Access-Control-Allow-Credentials: true');
header('access-control-expose-headers: AMP-Access-Control-Allow-Source-Origin');
header('AMP-Access-Control-Allow-Source-Origin: https://podnews.net');
header('Content-Type: application/json');
特别是:
$\u服务器['HTTP\u ORIGIN']
可以包括AMP缓存,据我所知


这里的正确有效值是什么?如何添加多个值(如果至少有两个AMP缓存)?为什么它不能在开发站点上运行,这类似于(它引发的错误是CORS错误,而不是关于使用HTTP而不是HTTPS的错误)。我怎样才能写出来让所有AMP开发人员都有一个简单的参考资料呢?

经过大量的修改,我认为答案是这里的代码相当笨拙:

header('Cache-Control: private, no-cache');

$thisDomain="https://podnews.net"; // The main production domain
$devDomain="http://dev.podnews.net"; // The development domain

$googleAMPCacheSubdomain=str_replace(".","-",str_replace("-","--",$thisDomain));

//If you use an IDN, you've got more work to do in the above to work out your AMP cache subdomain
//https://github.com/ampproject/amphtml/blob/master/spec/amp-cors-requests.md has details

$validOrigins=array('https://'.$googleAMPCacheSubdomain.'.cdn.ampproject.org','https://cdn.ampproject.org','https://amp.cloudflare.com',$thisDomain,$devDomain);

if (!in_array($_SERVER['HTTP_ORIGIN'],$validOrigins)) {
    header('X-Debug: '.$_SERVER['HTTP_ORIGIN'].' is an unrecognised origin');
    header('HTTP/1.0 403 Forbidden');exit;

    //Stop doing anything if this is an unfamiliar origin
}

if ($_GET['__amp_source_origin']!=$thisDomain AND $_GET['__amp_source_origin']!=$devDomain) {
    header('X-Debug: '.$_GET['__amp_source_origin'].' is an unrecognised source origin');
    header('HTTP/1.0 403 Forbidden');exit;

    //Stop doing anything if this is an unfamiliar source origin
    //Note: if using Amazon Cloudfront, don't forget to allow query strings through
}

header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN']);
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin');
header('AMP-Access-Control-Allow-Source-Origin: '.urldecode($_GET['__amp_source_origin']));
header('Content-Type: application/json');
// You're in!

我希望这是一个很好的复制/粘贴答案,其他人可能会觉得有用。这是一项艰巨的工作

这包含错误,因为“https://”。$googleAMPCacheSubdomain将在开始时包含两次https://