Created
February 14, 2019 08:56
-
-
Save iamaamir/8c5616d94bd301a5f49e2fb1a626d187 to your computer and use it in GitHub Desktop.
add a subscriber to mailchimp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (isset($_POST['email'])) { | |
$email = $_POST['email']; | |
$template = strtr("{\"email_address\": \"%email%\",\"status\": \"subscribed\"}", | |
array('%email%' => $email)); | |
$curl = curl_init(); | |
curl_setopt_array($curl, array( | |
CURLOPT_URL => "https://us20.api.mailchimp.com/3.0/lists/<list_id_goes_here>/members", | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_ENCODING => "", | |
CURLOPT_MAXREDIRS => 10, | |
CURLOPT_TIMEOUT => 30, | |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | |
CURLOPT_CUSTOMREQUEST => "POST", | |
CURLOPT_POSTFIELDS => $template, | |
CURLOPT_HTTPHEADER => array( | |
"Authorization: apikey xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" | |
), | |
)); | |
$response = curl_exec($curl); | |
$err = curl_error($curl); | |
curl_close($curl); | |
if ($err) { | |
echo "Something went wrong"; | |
} else { | |
$res = json_decode($response, true); | |
if ($res['status'] === 400) { | |
if (strpos($res['detail'], "already") !== false) { | |
echo "Emai Already Exist"; | |
} | |
else{ | |
echo $res['detail']; | |
} | |
}else if($res['email_address'] === $email){ | |
echo "Thank you for subscription"; | |
} | |
} | |
}else{ | |
echo "invalid Request"; | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>testing php post method</title> | |
</head> | |
<body> | |
<form id="form"> | |
<input type="text"> | |
<input type="submit" value="send"> | |
</form> | |
</body> | |
<script> | |
document.getElementById("form").addEventListener("submit", function(e) { | |
e.preventDefault(); | |
fetch("post.php", { | |
method: "POST", | |
body: new URLSearchParams("email=" + this.elements[0].value) | |
}) | |
.then(res => res.text()) | |
.then(res => console.log(res)); | |
return false; | |
}); | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment