44class EmailValidation
55{
66 private $ apiKey = '' ;
7- private $ singleValidationApiUrl = 'https://api.mailboxvalidator.com/v2/validation/single ' ;
8- private $ disposableEmailApiUrl = 'https://api.mailboxvalidator.com/v2/email/disposable ' ;
9- private $ freeEmailApiUrl = 'https://api.mailboxvalidator.com/v2/email/free ' ;
7+ private $ baseUrl = 'https://api.mailboxvalidator.com/v2/ ' ;
108
119 public function __construct ($ key )
1210 {
@@ -17,118 +15,72 @@ public function __destruct()
1715 {
1816
1917 }
20-
21- /*
22- * Custom wrapper function for CURL
23- */
24- public function curl ($ url ) {
25- // Initialize cURL session
26- $ ch = curl_init ();
27-
28- // Set cURL options
29- curl_setopt ($ ch , CURLOPT_URL , $ url );
30- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true ); // Return the response as a string instead of outputting it
3118
32- // Execute cURL session and store the response in a variable
33- $ response = curl_exec ($ ch );
19+ /**
20+ * Internal helper to handle API requests
21+ */
22+ private function request ($ path , $ email )
23+ {
24+ $ params = array (
25+ 'email ' => $ email ,
26+ 'key ' => $ this ->apiKey ,
27+ 'format ' => 'json ' ,
28+ 'source ' => 'sdk-php-mbv '
29+ );
3430
35- // Check for cURL errors
36- if (curl_errno ($ ch )) {
37- // echo 'cURL Error: ' . curl_error($ch);
38- return null ;
39- }
31+ $ url = $ this ->baseUrl . $ path . '? ' . http_build_query ($ params );
32+ $ results = $ this ->curl ($ url );
4033
41- // Close cURL session
42- curl_close ($ ch );
43-
44- return $ response ;
45- }
46-
47- /*
48- * Validate whether an email address is a valid email or not.
49- */
50- public function validateEmail ($ email )
34+ if ($ results ) {
35+ return json_decode ($ results );
36+ }
37+
38+ return null ;
39+ }
40+
41+ /**
42+ * Custom wrapper function for CURL
43+ */
44+ public function curl ($ url )
5145 {
52- try {
53- $ params = [ 'email ' => $ email , 'key ' => $ this ->apiKey , 'format ' => 'json ' , 'source ' => 'sdk-php-mbv ' ];
54- $ params2 = [];
55- foreach ($ params as $ key => $ value ) {
56- $ params2 [] = $ key . '= ' . rawurlencode ($ value );
57- }
58- $ params = implode ('& ' , $ params2 );
59-
60- $ results = $ this ->curl ($ this ->singleValidationApiUrl . '? ' . $ params );
61-
62- if ($ results !== false ) {
63- return json_decode ($ results );
64- }
65- else {
66- return null ;
67- }
68- } catch (Exception $ e ) {
46+ $ ch = curl_init ();
47+ curl_setopt ($ ch , CURLOPT_URL , $ url );
48+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
49+ // Optimization: Added a timeout to prevent script hanging
50+ curl_setopt ($ ch , CURLOPT_TIMEOUT , 30 );
51+
52+ $ response = curl_exec ($ ch );
53+
54+ if (curl_errno ($ ch )) {
55+ curl_close ($ ch );
6956 return null ;
7057 }
71- // restore_error_handler();
58+
59+ curl_close ($ ch );
60+ return $ response ;
7261 }
7362
74- /*
75- * Validate whether an email address is a disposable email or not.
76- */
63+ /**
64+ * Validate whether an email address is a valid email or not.
65+ */
66+ public function validateEmail ($ email )
67+ {
68+ return $ this ->request ('validation/single ' , $ email );
69+ }
70+
71+ /**
72+ * Validate whether an email address is a disposable email or not.
73+ */
7774 public function isDisposableEmail ($ email )
7875 {
79- try {
80- $ params = [ 'email ' => $ email , 'key ' => $ this ->apiKey , 'format ' => 'json ' , 'source ' => 'sdk-php-mbv ' ];
81- $ params2 = [];
82- foreach ($ params as $ key => $ value ) {
83- $ params2 [] = $ key . '= ' . rawurlencode ($ value );
84- }
85- $ params = implode ('& ' , $ params2 );
86-
87- $ results = $ this ->curl ($ this ->disposableEmailApiUrl . '? ' . $ params );
88-
89- if ($ results !== false ) {
90- return json_decode ($ results );
91- }
92- else {
93- return null ;
94- }
95- } catch (Exception $ e ) {
96- return null ;
97- }
76+ return $ this ->request ('email/disposable ' , $ email );
9877 }
9978
100- /*
101- * Validate whether an email address is a free email or not.
102- */
79+ /**
80+ * Validate whether an email address is a free email or not.
81+ */
10382 public function isFreeEmail ($ email )
10483 {
105- try {
106- $ params = [ 'email ' => $ email , 'key ' => $ this ->apiKey , 'format ' => 'json ' , 'source ' => 'sdk-php-mbv ' ];
107- $ params2 = [];
108- foreach ($ params as $ key => $ value ) {
109- $ params2 [] = $ key . '= ' . rawurlencode ($ value );
110- }
111- $ params = implode ('& ' , $ params2 );
112-
113- $ results = $ this ->curl ($ this ->freeEmailApiUrl . '? ' . $ params );
114-
115- if ($ results !== false ) {
116- return json_decode ($ results );
117- }
118- else {
119- return null ;
120- }
121- } catch (Exception $ e ) {
122- return null ;
123- }
84+ return $ this ->request ('email/free ' , $ email );
12485 }
125-
126- /*private function ()
127- {
128- set_error_handler(
129- function ($severity, $message, $file, $line) {
130- throw new ErrorException($message, $severity, $severity, $file, $line);
131- }
132- );
133- }*/
13486}
0 commit comments