<?php
include_once "../dbconection.php";

//include "funciones.php";

// Get remote file contents, preferring faster cURL if available

function remote_get_contents($url){
	return file_get_contents($url);
}

function curl_get_contents($url){

    // Initiate the curl session
    $ch = curl_init();
	//$ch = curl_init($url);
	//exit(curl_errno($ch));
	
    // Set the URL
    curl_setopt($ch, CURLOPT_URL, $url);
    
    // Removes the headers from the output
    //curl_setopt($ch, CURLOPT_HEADER, false);

	curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json','Content-Type: application/json'));
    
    // Return the output instead of displaying it directly
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    // Execute the curl session
    $output = curl_exec( $ch );
	
	$info = curl_getinfo( $ch );
	print_r($info);
	
	//print_r(curl_error($ch));
//		if ($output === false || $info['http_code'] != 200) {
//		  $output = "No cURL data returned for $url [". $info['http_code']. "]";
//		  if (curl_error($ch))
//			$output .= "\n". curl_error($ch);
//		}else {
//		  // 'OK' status; format $output data if necessary here:
//		  print_r($info);
//		}
	
	//$sAverageSpeedDownload = curl_getinfo( $ch, CURLINFO_SPEED_DOWNLOAD );
	//$sAverageSpeedUpload   = curl_getinfo( $ch, CURLINFO_SPEED_UPLOAD );
	
//		echo '<pre>';
//		echo 'Info<br>';
//		//print_r($info);
//		echo '______<br>';
//		//echo 'Average speed download == ' . $sAverageSpeedDownload . '<br>';
//		//echo 'Average Speed upload    == ' . $sAverageSpeedUpload   . '<br>';
//		echo '</pre>';
	
	
    // Close the curl session
    curl_close($ch);
    
	//exit("output = ". $output);
	
    // Return the output as a variable
    return $output;
}

/*
function verificarEmail($email){
	global $mysqli;

	$url = 'https://app.verify-email.org/api/v1/qZT2dwCq1wPc0GzSr7zxDGoLcE3xfqxzHOpNbUlX0msOSojvDr/verify/'. $email;
	
	$sJson = file_get_contents($url);
	
	//exit($sJson);
	
	$object	= json_decode($sJson);

	//print((string)$object);
	
	//echo '<div id="response">';
	//VALIDO
	//{"email":"25eaglejage@gmail.com","status":1,"status_description":"OK email","smtp_code":250,"smtp_log":"Success"}

	//NO VALIDO
	//{"email":"25eaglejage@gmuytt6yail.com","status":0,"status_description":"invalid domain","smtp_code":-1,"smtp_log":"Domain does not exist.\r MX record about this domain does not exist.\r"}

	//NO VALIDO
	//{"email":"25eaglejag898989998iioioe@gmail.com","status":0,"status_description":"BAD email","smtp_code":500,"smtp_log":"MailboxDoesNotExist"}
	
	$strReturnnn = "";
	
	if(isset($object->status)){

		if( $object->status==1 ){
			
			$query = "UPDATE tblmedicos2020 SET intEmailValido=1 WHERE strEmail = '". $email ."'";
			
			$strReturnnn = ' is GOOD ________';
		
		}else{

			$query = "UPDATE tblmedicos2020 SET intEmailValido=0 WHERE strEmail = '". $email ."'";
			$strReturnnn = ' is BAD ________';

		}

		//$CON->execute($query);
		$queryResult = $mysqli->query($query);

		//exit($query);

	}else{

		$strReturnnn = 'La propiedad object->status no existe ________';
		
	}

	return $strReturnnn;
	
}
*/

function verificarEmailDebounce($email){
   	
   	global $mysqli;
 
	$url = 'https://api.debounce.io/v1/?api=5fab3bab5023c&email='. $email;
	
	//$sJson = remote_get_contents($url);
	$sJson = file_get_contents($url);
	
	//exit("url: ". $url ." / JSON: ". $sJson);
	
	$object	= json_decode($sJson);

	//VALIDO
	//{"debounce":{"email":"25eaglejage@gmail.com","code":"5","role":"false","free_email":"true","result":"Safe to Send","reason":"Deliverable","send_transactional":"1","did_you_mean":""},"success":"1","balance":"99"}

	//NO VALIDO
	//{"debounce":{"email":"25eaglejag23323e@gmail.com","code":"6","role":"false","free_email":"true","result":"Invalid","reason":"Bounce","send_transactional":"0","did_you_mean":""},"success":"1","balance":"98"}

	//NO VALIDO
	//{"debounce":{"email":"25eaglejage@geeemail.com","code":"4","role":"false","free_email":"false","result":"Risky","reason":"Accept All","send_transactional":"1","did_you_mean":""},"success":"1","balance":"97"}

	$strReturnnn = "";
	
	if(1==1){
	    
		//$result = $object->debounce->result;
		//$result = $object->debounce->reason;

		//$result = $object->{'debounce'}->{'reason'};
		
		$debounce = $object->{'debounce'};
		$result = $debounce->{'reason'};
		
		if($result == "Deliverable"){
					
			$query = "UPDATE tblmedicos2020 SET intEmailValido=1 WHERE strEmail = '". $email ."' ";
			$strReturnnn = ' is GOOD ________';
			
		}else{

			$query = "UPDATE tblmedicos2020 SET intEmailValido=0 WHERE strEmail = '". $email ."' ";
			$strReturnnn = ' is BAD ________';

		}

		$queryResult = $mysqli->query($query);
        //exit($query);

	}else{

		$strReturnnn = 'La propiedad object->debounce->result no existe ________';
		
	}
	
	return $strReturnnn;
}


$query = "SELECT strEmail FROM tblmedicos2020 WHERE strEmail IS NOT NULL AND intEmailValido IS NULL ORDER BY strEmail ASC Limit 0,100";

//exit($query);

$totalRows = 0;

$queryResult = $mysqli->query($query);
$totalRows = mysqli_num_rows($queryResult);

if($totalRows > 0){
	$iLinea = 1;

	while($data = mysqli_fetch_assoc($queryResult)){
	    
		$strEmail = $data['strEmail'];
		
		$strReturn = "NULO";
		$strReturn = verificarEmailDebounce($strEmail);

		print($iLinea .".- email: ". $strEmail ." / strReturn: ". $strReturn ."<br/>");
		
		usleep(50);

		$iLinea++;
		
	}
}else{
    print("No hay correos por verificar");
}

mysqli_free_result($queryResult);

/*
if(!$rs->EOF){
	
	$iLinea = 1;

	while(!$rs->EOF){

		//$email = $rs->Fields($i)->mail;
		$email = $rs->Fields(0)->value;
		
		$strReturn = "NULO";
		$strReturn = verificarEmail2($email);

		print($iLinea .".- email: ". $email ." / strReturn: ". $strReturn ."<br/>");
		
		usleep(50);

		$iLinea++;
		
		$rs->MoveNext();
		
	}// CIERRA WHILE

}

$rs->Close();
$CON->Close();

$rs = null;
$CON = null;
*/

?>

