Skip to content

Commit

Permalink
Update to support for v2 API.
Browse files Browse the repository at this point in the history
  • Loading branch information
MailboxValidator committed Sep 12, 2023
1 parent 231f2ee commit 4254086
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 36 deletions.
52 changes: 26 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Add the following to your composer.json file:

```
"require": {
"mailboxvalidator/mailboxvalidator-php": "2.0.*"
"mailboxvalidator/mailboxvalidator-php": "2.1.*"
}
```

Expand Down Expand Up @@ -115,29 +115,29 @@ $results = $mbv->validateEmail('[email protected]');
if ($results === null) {
echo "Error connecting to API.\n";
}
else if (trim($results->error_code) === '') {
else if (!isset($results->error)) {
echo 'email_address = ' . $results->email_address . "\n";
echo 'domain = ' . $results->domain . "\n";
echo 'is_free = ' . $results->is_free . "\n";
echo 'is_syntax = ' . $results->is_syntax . "\n";
echo 'is_domain = ' . $results->is_domain . "\n";
echo 'is_smtp = ' . $results->is_smtp . "\n";
echo 'is_verified = ' . $results->is_verified . "\n";
echo 'is_server_down = ' . $results->is_server_down . "\n";
echo 'is_greylisted = ' . $results->is_greylisted . "\n";
echo 'is_disposable = ' . $results->is_disposable . "\n";
echo 'is_suppressed = ' . $results->is_suppressed . "\n";
echo 'is_role = ' . $results->is_role . "\n";
echo 'is_high_risk = ' . $results->is_high_risk . "\n";
echo 'is_catchall = ' . $results->is_catchall . "\n";
echo 'is_free = ' . var_export($results->is_free, true) . "\n";
echo 'is_syntax = ' . var_export($results->is_syntax, true) . "\n";
echo 'is_domain = ' . var_export($results->is_domain, true) . "\n";
echo 'is_smtp = ' . var_export($results->is_smtp, true) . "\n";
echo 'is_verified = ' . var_export($results->is_verified, true) . "\n";
echo 'is_server_down = ' . var_export($results->is_server_down, true) . "\n";
echo 'is_greylisted = ' . var_export($results->is_greylisted, true) . "\n";
echo 'is_disposable = ' . var_export($results->is_disposable, true) . "\n";
echo 'is_suppressed = ' . var_export($results->is_suppressed, true) . "\n";
echo 'is_role = ' . var_export($results->is_role, true) . "\n";
echo 'is_high_risk = ' . var_export($results->is_high_risk, true) . "\n";
echo 'is_catchall = ' . var_export($results->is_catchall, true) . "\n";
echo 'mailboxvalidator_score = ' . $results->mailboxvalidator_score . "\n";
echo 'time_taken = ' . $results->time_taken . "\n";
echo 'status = ' . $results->status . "\n";
echo 'status = ' . var_export($results->status, true) . "\n";
echo 'credits_available = ' . $results->credits_available . "\n";
}
else {
echo 'error_code = ' . $results->error_code . "\n";
echo 'error_message = ' . $results->error_message . "\n";
echo 'error_code = ' . $results->error->error_code . "\n";
echo 'error_message = ' . $results->error->error_message . "\n";
}
?>
```
Expand All @@ -158,14 +158,14 @@ $results = $mbv->isDisposableEmail('[email protected]');
if ($results === null) {
echo "Error connecting to API.\n";
}
else if (trim($results->error_code) === '') {
else if (!isset($results->error)) {
echo 'email_address = ' . $results->email_address . "\n";
echo 'is_disposable = ' . $results->is_disposable . "\n";
echo 'is_disposable = ' . var_export($results->is_disposable, true) . "\n";
echo 'credits_available = ' . $results->credits_available . "\n";
}
else {
echo 'error_code = ' . $results->error_code . "\n";
echo 'error_message = ' . $results->error_message . "\n";
echo 'error_code = ' . $results->error->error_code . "\n";
echo 'error_message = ' . $results->error->error_message . "\n";
}
?>
```
Expand All @@ -185,14 +185,14 @@ $results = $mbv->isFreeEmail('[email protected]');
if ($results === null) {
echo "Error connecting to API.\n";
}
else if (trim($results->error_code) === '') {
else if (!isset($results->error)) {
echo 'email_address = ' . $results->email_address . "\n";
echo 'is_free = ' . $results->is_free . "\n";
echo 'is_free = ' . var_export($results->is_free, true) . "\n";
echo 'credits_available = ' . $results->credits_available . "\n";
}
else {
echo 'error_code = ' . $results->error_code . "\n";
echo 'error_message = ' . $results->error_message . "\n";
echo 'error_code = ' . $results->error->error_code . "\n";
echo 'error_message = ' . $results->error->error_message . "\n";
}
?>
```
Expand All @@ -212,4 +212,4 @@ Errors
Copyright
=========

Copyright(C) 2018-2020 by MailboxValidator.com.
Copyright(C) 2018-2023 by MailboxValidator.com.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@
"FraudLabsPro\\Test\\": "tests/"
}
},
"version": "2.0.0"
"version": "2.1.0"
}
54 changes: 45 additions & 9 deletions src/EmailValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
class EmailValidation
{
private $apiKey = '';
private $singleValidationApiUrl = 'https://api.mailboxvalidator.com/v1/validation/single';
private $disposableEmailApiUrl = 'https://api.mailboxvalidator.com/v1/email/disposable';
private $freeEmailApiUrl = 'https://api.mailboxvalidator.com/v1/email/free';
private $singleValidationApiUrl = 'https://api.mailboxvalidator.com/v2/validation/single';
private $disposableEmailApiUrl = 'https://api.mailboxvalidator.com/v2/email/disposable';
private $freeEmailApiUrl = 'https://api.mailboxvalidator.com/v2/email/free';

public function __construct($key)
{
Expand All @@ -17,21 +17,47 @@ public function __destruct()
{

}

/*
* Custom wrapper function for CURL
*/
public function curl($url) {
// Initialize cURL session
$ch = curl_init();

// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the response as a string instead of outputting it

// Execute cURL session and store the response in a variable
$response = curl_exec($ch);

// Check for cURL errors
if (curl_errno($ch)) {
// echo 'cURL Error: ' . curl_error($ch);
return null;
}

// Close cURL session
curl_close($ch);

return $response;
}

/*
* Validate whether an email address is a valid email or not.
*/
public function validateEmail($email)
{
try {
$params = [ 'email' => $email, 'key' => $this->apiKey, 'format' => 'json' ];
$params = [ 'email' => $email, 'key' => $this->apiKey, 'format' => 'json', 'source' => 'sdk-php-mbv' ];
$params2 = [];
foreach ($params as $key => $value) {
$params2[] = $key . '=' . rawurlencode($value);
}
$params = implode('&', $params2);

$results = file_get_contents($this->singleValidationApiUrl . '?' . $params);
$results = $this->curl($this->singleValidationApiUrl . '?' . $params);

if ($results !== false) {
return json_decode($results);
Expand All @@ -42,6 +68,7 @@ public function validateEmail($email)
} catch (Exception $e) {
return null;
}
// restore_error_handler();
}

/*
Expand All @@ -50,14 +77,14 @@ public function validateEmail($email)
public function isDisposableEmail($email)
{
try {
$params = [ 'email' => $email, 'key' => $this->apiKey, 'format' => 'json' ];
$params = [ 'email' => $email, 'key' => $this->apiKey, 'format' => 'json', 'source' => 'sdk-php-mbv' ];
$params2 = [];
foreach ($params as $key => $value) {
$params2[] = $key . '=' . rawurlencode($value);
}
$params = implode('&', $params2);

$results = file_get_contents($this->disposableEmailApiUrl . '?' . $params);
$results = $this->curl($this->disposableEmailApiUrl . '?' . $params);

if ($results !== false) {
return json_decode($results);
Expand All @@ -76,14 +103,14 @@ public function isDisposableEmail($email)
public function isFreeEmail($email)
{
try {
$params = [ 'email' => $email, 'key' => $this->apiKey, 'format' => 'json' ];
$params = [ 'email' => $email, 'key' => $this->apiKey, 'format' => 'json', 'source' => 'sdk-php-mbv' ];
$params2 = [];
foreach ($params as $key => $value) {
$params2[] = $key . '=' . rawurlencode($value);
}
$params = implode('&', $params2);

$results = file_get_contents($this->freeEmailApiUrl . '?' . $params);
$results = $this->curl($this->freeEmailApiUrl . '?' . $params);

if ($results !== false) {
return json_decode($results);
Expand All @@ -95,4 +122,13 @@ public function isFreeEmail($email)
return null;
}
}

/*private function ()
{
set_error_handler(
function ($severity, $message, $file, $line) {
throw new ErrorException($message, $severity, $severity, $file, $line);
}
);
}*/
}

0 comments on commit 4254086

Please sign in to comment.