Created
February 20, 2015 13:46
-
-
Save stephenkeable-allies/337cac76644b3808ee68 to your computer and use it in GitHub Desktop.
Basic PHP Email Validation Example using PostCoder Web. Find out more at - http://developers.alliescomputing.com/postcoder-web-api/email-validation
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 | |
// Check if POST request has been made | |
if($_SERVER['REQUEST_METHOD'] === 'POST') { | |
// Basic validation on the email field | |
if(array_key_exists('email', $_POST) and !empty($_POST['email'])) { | |
// Insert your PostCoder Web API Key here | |
// Sign up for free at www.alliescomputing.com/postcoder/sign-up | |
$pcw_api_key = "YOUR-API-KEY-HERE"; | |
// URL we are calling with API key and url encoded email | |
$pcw_validate_url = "http://ws.postcoder.com/pcw/". $pcw_api_key ."/email/" . rawurlencode($_POST['email']); | |
// Get and JSON decode the response from the service | |
$response = json_decode(file_get_contents($pcw_validate_url)); | |
// Was the response TRUE or FALSE, show the correct message | |
if($response->valid === FALSE) { | |
echo "<p><strong>" . $_POST['email'] . "</strong> - IS NOT a valid email address</p>"; | |
} else { | |
echo "<p><strong>" . $_POST['email'] . "</strong> - IS a valid email address</p>"; | |
} | |
// Certain domains only allow minimal validation, if so this warning will be present | |
if(isset($response->warning)) { | |
echo "<p>Warning: " . $response->warning . "</p>"; | |
} | |
} | |
} | |
// Basic POST submit form HTML below | |
?> | |
<form action="email-validation.php" method="post"> | |
<fieldset> | |
<label for="email">Email Address:</label> | |
<input type="text" name="email" id="email"> | |
</fieldset> | |
<input type="submit" value="Validate"> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment