Código PHP:
/**
* Obtiene el buffer y establece los resultados. Esta funcion debe de ser llamada despues de crear el objeto.
* @return Bool True en success, false en error.
*/
function obtainExchangeR(){
if(count($this->regex)!= count($this->resources))return false;
if($this->setBuffer()) return $this->setResult();
$this->debug_log("setBuffer() Failed at line ".__LINE__,"data",__METHOD__);
return false;
}
/**
* Establece en el log un error de mysql, y para la ejecucion del php con un error y un numero de linea.
* @return bool
*/
protected function dieMsg(){
$this->debug_log($_SERVER['PHP_SELF']." MYSQL_ERROR: ".mysql_error($this->connection)." on line ".__LINE__,"sql",__METHOD__);
die($_SERVER['PHP_SELF']." MYSQL_ERROR: ".mysql_error($this->connection)." on line: ".__LINE__);
return false;
}
/**
* Inserta un registro en la base de datos con el tipo de cambio al dia. <b>Esta funcion debera de estar en clase separada para la siguiente version. Seran 3 capas de abstaccion de datos.</b>
* @return
*/
function setToDatabase(){
if($this->code!='200'||is_null($this->fechaCambio)||is_null($this->tipoCambio)){
$this->debug_log("There is no data to insert to database.",'sql',__METHOD__);
return false;
}
$query = "INSERT INTO com_tiposcambio (TipCam_idMoneda ,TipCam_Fecha, TipCam_ContraYUAN) VALUES (2,'$this->fechaCambio','$this->tipoCambio')";
$RTipoCambio = mysql_query($query,$this->connection) or $this->dieMsg();
if(mysql_affected_rows($this->connection)>0)
return true;
else return false;
}
/**
* Funcion que escribe en un archivo llamado log.txt
* @param String $txt_to_log Mensaje a escribir al log
* @return Bool
*/
protected function debug_log($txt_to_log,$error_type='standard',$user='system'){
$date = date("D M d H:i:s Y");
error_log("[$date] [$error_type] [user: $user] $txt_to_log \n", 3, LOG_PATH);
}
}
para usarla:
Código PHP:
$ExcRt = new ExchangeRate($connection);
if($ExcRt->obtainExchangeR())
echo ($ExcRt->setToDatabase())?"Se obtuvo el tipo de cambio y se guardo en la base de datos.":"Se obtuvo el tipo de cambio pero no se guardo en la base de datos. Consultar el log.txt";
else
echo "No fue posible obtener el tipo de cambio. Consultar el log.txt";
echo "<br />Servicio en: ".$_SERVER['SERVER_NAME']." @ ".$_SERVER['SERVER_ADDR'];