<?php
class Banxico

{

    const banxicourl = 'http://www.banxico.org.mx:80/DgieWSWeb/DgieWS?WSDL';

    private $_client;

    private $_debug = false;

    public function getExRate()

    {

        $client = $this->_getClient();

        try

        {

            $result = $client->tiposDeCambioBanxico();

        }

        catch (SoapFault $e)

        {

            return $e->getMessage();

        }

        if(!empty($result))

        {

            $dom = new DOMDocument();

            $dom->loadXML($result);

            $xpath = new DOMXPath($dom);

            $xpath->registerNamespace('bm', "http://ws.dgie.banxico.org.mx");

            $val = $xpath->evaluate("//*[@IDSERIE='SF60653']/*/@OBS_VALUE");

            return ($val->item(0)->value);

        }

    }

    /**

     * @return SoapClient

     */

    private function _getClient()

    {

        if(empty($this->_client)) {

            $this->_client = $this->_setClient();

        }

        return $this->_client;

    }

    /**

     * @return SoapClient

     */

    private function _setClient()

    {

        return new SoapClient(null,

            array('location' => self::banxicourl,

            'uri'      => 'http://DgieWSWeb/DgieWS?WSDL',

            'encoding' => 'ISO-8859-1',

            'trace'    => $this->_getDebug()

            ));

    }

    private function _getDebug()

    {

        return $this->_debug;

    }

}

 

$dato = new Banxico();

$convmxn = 19.40;//round($dato->getExRate(),2);


//Registra tipo de cambio
	 $fechaActual=date("Y-m-d");
	 $tc2=$convmxn+(0.40);
	 //$tc2=str_replace(",",".",$tc);
	 $convmxn=$tc2;
	 //Consulta si ya se registro el TC del dia
	 $sql="SELECT COUNT(idtipocam) AS no FROM tipodecambio WHERE fecha='$fechaActual'";
	 $con=mysql_query($sql);
	 $res=mysql_fetch_array($con);
	 //Si no se ha registrado el TC se inserta
	 if($res["no"]==0){
	  $sqltipo="INSERT INTO tipodecambio(fecha,tipocambio) values('".$fechaActual."','".$tc2."')";
	  mysql_query($sqltipo);
	 }
	 //Si en la pagina de DOF no hay TC obtengo el último TC que se registró 
	 if($tc2<1){
       $sqlIDTC="SELECT MAX(idtipocam) as ID FROM tipodecambio WHERE tipocambio > 1";
	   $conIDTC=mysql_query($sqlIDTC);
	   $ejeIDTC=mysql_fetch_array($conIDTC);
	   $IdTC=$ejeIDTC['ID'];
	   
	   $sqlTC="SELECT tipocambio FROM tipodecambio WHERE idtipocam = '$IdTC'";
	   $conTC=mysql_query($sqlTC);
	   $ejeTC=mysql_fetch_array($conTC);
	   $convmxn=$ejeTC['tipocambio'];
     }
?>



