Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Description: This is where the email address given will be posted once the user has filled it in on the newsletter widget
- */
- // Include file with auth vars
- require_once('../../../wp-config.php');
- // Define variables
- $useremail = "";
- $apiemail = get_option('apiemail');
- $apipassword = get_option('apipassword');
- $listID = get_option('listID');
- $xml_string = "";
- $input_unsuccessful_slug = get_option('slugUnsuccess');
- $input_successful_slug = get_option('slugSuccess');
- // Tidies slug slashes
- $unsuccessful_slug = str_replace('/', '', $input_unsuccessful_slug);
- $successful_slug = str_replace('/', '', $input_successful_slug);
- // Create un/success URLs
- $success_url = site_url() . '/' . $successful_slug;
- $unsuccess_url = site_url() . '/' . $unsuccessful_slug;
- // Test for validation
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- $useremail = test_input($_POST["useremail"]);
- }
- // Tidy input
- function test_input($data) {
- $data = trim($data);
- $data = stripslashes($data);
- $data = htmlspecialchars($data);
- return $data;
- }
- // Sanitise email
- $clean_useremail = filter_var($useremail, FILTER_SANITIZE_EMAIL);
- // Build URL
- $auth_url = 'https://api.dotmailer.com/v2/address-books/' . $listID .'/contacts';
- // Create the XML data to be added
- $xml = array(
- 'id' => 'generic',
- 'email' => $clean_useremail,
- 'optInType' => 'Unknown',
- 'emailType' => 'Html',
- 'dataFields' => ':null',
- 'status' => 'Subscribed'
- );
- // JSON
- $xml_string = json_encode($xml);
- // The request
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL,$auth_url);
- curl_setopt($ch,CURLOPT_POST, count($xml));
- curl_setopt($ch,CURLOPT_POSTFIELDS, $xml_string);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
- curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
- curl_setopt($ch, CURLOPT_USERPWD, "$apiemail:$apipassword");
- curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- 'Content-Type: application/json',
- 'Content-Length: ' . strlen($xml_string))
- );
- $result = curl_exec ($ch);
- $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code
- curl_close ($ch);
- // User feedback
- if (isset($status_code)) {
- if ($status_code == '200'){
- header('Location:' . $success_url);
- }
- else if ($status_code =='400'){
- header('Location:' . $unsuccess_url);
- }
- else{
- echo 'Submission success undetermined. HTTP response = ' . $status_code . '<br />Please contact the site administrator';
- }
- };
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement