Using PHP Code In Cloudflare Workers For CORS Headers

I have an AMP blog on a CMS that does not allow me access to the root (Blogger/Blogspot). This has been a limitation so far but I have managed to keep going with AMP because I have come too far to turn back.

Here is one of the issues I am struggling with:
I am trying to implement a Mailchimp subscription form on the website (sample link: Sample Page, but I get the error that CORS headers are blocked because amp-form does not allow x-domain interaction or whatever.

I found several solutions out there, but there is one I thought would be the easiest has a PHP code that I should upload to my domain source, but like I mentioned already, Blogger does not allow me to upload anything to the source. So, I saw that it is possible to execute certain scripts on the edge on Cloudflare (Javascript, etc).
Please, does anyone know how I can make this work so that my AMP for can work fine?
Below is the PHP script;

<?php
if (!empty($_POST)) {
header("access-control-allow-credentials:true");
header("access-control-allow-headers:Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token");
header("access-control-allow-methods:POST, GET, OPTIONS");
header("access-control-allow-origin:".$_SERVER['HTTP_ORIGIN']);
header("access-control-expose-headers:AMP-Access-Control-Allow-Source-Origin");
header("amp-access-control-allow-source-origin:https://".$_SERVER['HTTP_HOST']);
header("Content-Type: application/json");
$email = isset($_POST['email']) ? $_POST['email'] : '';
$output = ['email' => $email];
header("Content-Type: application/json");
echo json_encode($output);
$post_data['u'] = 'c3dbdee25359e880a9e13dc9b';
$post_data['id'] = '56d83be183';
$post_data['b_email'] = urlencode($_POST['email']);
foreach($post_data as $key => $value) {
$post_items[] = $key.
'='.$value;
}
$post_string = implode('&', $post_items);
$curl_connection = curl_init('https://blogspot.us20.list-manage.com/subscribe/post');
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
curl_exec($curl_connection);
curl_close($curl_connection);
}
?>

I will appreciate any alternative solutions.
Thank you in advance for your help.

PS: Blogger does not also accept third-party plugins.

Pingback:
I want to host that script to the root of my domain using Cloudflare workers.

I am not aware if Cloudflare Workers accept PHP, rather be it JavaScript style:

Or just for example:

async function handleRequest(request) {
  return new Response(html, {
    headers: {
      "content-type": "text/html;charset=UTF-8",
      "Access-Control-Allow-Origin": "*",
    },
  })
}

In terms of Google AMP, Cloudflare has got an option AMP Real URL which we can enable in the Cloudflare Dashboard → Speed → Optimization → scroll down to the section AMP Real URL.

Nevertheless, I only use one HTTP Header as far as AMP articles are serverd from my domain URLs like www.domain.com/article/amp/:

amp-access-control-allow-source-origin: https://www.mydomain.com

async function handleRequest(request) {
  return new Response(html, {
    headers: {
      "content-type": "text/html;charset=UTF-8",
      "Access-Control-Allow-Origin": "*",
      "amp-access-control-allow-source-origin": "https://www.yourdomain.com/"
    },
  })
}

I am quite not sure if that could be supported, but it’s worth to try out.

You can route them for specific URLs using * in case if needed.

Helpful article at least for a bit in your case:

From my above example, of course, this could not be suitable for everyone in terms of security, context being used, origin requests protection etc.

May I ask how does your AMP URL look like for example?

Thank you very much for the help, Fritex. I will try them out asap.
To your question, my AMP Url looks like this:

https://www.knowseeker.com/article?m=1

Thank you very much!