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.
