how to read an external wsdl file in php

I have this wsdl file : http://www.banxico.org.mx/DgieWSWeb/DgieWS?WSDL

I need to create a php program that catch in real time the exchange rate from USD dollar to MXN mexican pesos.

The wsdl file is from "banco de mexico".

From this file the exchange rate is on the element "tiposDeCambioBanxico":
<wsdl:operation name="tiposDeCambioBanxico">
  <wsdlsoap:operation soapAction=""/>
    <wsdl:input name="tiposDeCambioBanxicoRequest">
        <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://ws.dgie.banxico.org.mx" use="encoded"/>
   </wsdl:input>
   <wsdl:output name="tiposDeCambioBanxicoResponse">
       <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://ws.dgie.banxico.org.mx" use="encoded"/>
   </wsdl:output>
</wsdl:operation>

Open in new window


With this code I try to read it
    require_once('lib/nusoap.php');
    
    $oSoapClient = new nusoap_client('http://www.banxico.org.mx:80/DgieWSWeb/DgieWS?WSDL','wsdl');
        
    if ($sError = $oSoapClient->getError()) {
        echo "No se pudo realizar la operación [" . $sError . "]";
        die();
    }

    $respuesta = $oSoapClient->call("tiposDeCambioBanxico");

    if ($oSoapClient->fault) { 
        echo 'No se pudo completar la operación';
        die();
    }else { 
        $sError = $oSoapClient->getError();

        if ($sError) { 
            echo 'Error:' . $sError;
            die();
        }
        echo "Funciono correctamente<br/>";
        echo "<pre>";print_r($respuesta,false); echo "</pre>";
        echo $respuesta["OBS_VALUE"];        
    } 

Open in new window


It seems to work but no matter what I do It always show me the following:
---------------------------------------------------------------------------------------------------------
            TIPOSDECAMBIO
            false
            false
            Tipos de Cambio
            2015-09-10 13:31:36.236
            
                  Banco de México
                  
                  Subgerencia de Desarrollo de Sistemas
                  (01 55)52372678
                  
            
            Update
            2015-09-10 13:31:36.236
------------------------------------------------------------------------------------
What Im doing wrong???

What else do I need to do catch the exchange rate(TIPOSDECAMBIO)

Best regards

Carlos Alvarez
altariamx2003Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
See if this helps.  Most of us who have had to use SOAP at one time or another hate SOAP.  If you can get this information in a JSON string, it's much more straightforward.
http://iconoun.com/demo/temp_carlos.php

The strategy here is to get the XML namespace out of the way so we can process the data with SimpleXML.  I can't speak to the quality of the data, but this seems to find the internal information that comes from the API.
<?php // demo/temp_carlos.php

/**
 * http://www.experts-exchange.com/questions/28712894/how-to-read-an-external-wsdl-file-in-php.html
 *
 * http://php.net/manual/en/book.simplexml.php
 */
error_reporting(E_ALL);
echo '<meta charset="utf-8" />';
echo '<pre>';

require_once('nusoap/lib/nusoap.php');

$oSoapClient = new nusoap_client('http://www.banxico.org.mx:80/DgieWSWeb/DgieWS?WSDL','wsdl');

if ($sError = $oSoapClient->getError()) {
    echo "No se pudo realizar la operación [" . $sError . "]";
    die();
}

$respuesta = $oSoapClient->call("tiposDeCambioBanxico");

if ($oSoapClient->fault) {
    echo 'No se pudo completar la operación';
    die();
}else {
    $sError = $oSoapClient->getError();

    if ($sError) {
        echo 'Error:' . $sError;
        die();
    }
}


// MUNG THE XML NAMESPACE INTO A SET OF XML TAGS
$resp = mungxml($respuesta);

// NOW WE CAN USE SIMPLEXML
$obj  = SimpleXML_Load_String($resp);

// ACTIVATE THIS TO LOOK AT THE ENTIRE OBJECT
// var_dump($obj);

// USE AN ITERATOR TO ACCESS DATA INSIDE THE OBJECT
foreach ($obj->bm_DataSet->bm_Series as $data)
{
    echo PHP_EOL;
    echo $data['TITULO'];
    echo PHP_EOL;
    echo $data->bm_Obs['TIME_PERIOD']. ' ';
    echo $data['IDSERIE'] . ' ';
    echo $data->bm_Obs['OBS_VALUE'];
    echo PHP_EOL;
}


// FUNCTION TO MUNG THE XML SO WE DO NOT HAVE TO DEAL WITH NAMESPACE
function mungXML($xml)
{
    $obj = SimpleXML_Load_String($xml);
    if ($obj === FALSE) return $xml;

    // GET NAMESPACES, IF ANY
    $nss = $obj->getNamespaces(TRUE);
    if (empty($nss)) return $xml;

    // CHANGE ns: INTO ns_
    $nsm = array_keys($nss);
    foreach ($nsm as $key)
    {
        // A REGULAR EXPRESSION TO MUNG THE XML
        $rgx
        = '#'               // REGEX DELIMITER
        . '('               // GROUP PATTERN 1
        . '\<'              // LOCATE A LEFT WICKET
        . '/?'              // MAYBE FOLLOWED BY A SLASH
        . preg_quote($key)  // THE NAMESPACE
        . ')'               // END GROUP PATTERN
        . '('               // GROUP PATTERN 2
        . ':{1}'            // A COLON (EXACTLY ONE)
        . ')'               // END GROUP PATTERN
        . '#'               // REGEX DELIMITER
        ;
        // INSERT THE UNDERSCORE INTO THE TAG NAME
        $rep
        = '$1'          // BACKREFERENCE TO GROUP 1
        . '_'           // LITERAL UNDERSCORE IN PLACE OF GROUP 2
        ;
        // PERFORM THE REPLACEMENT
        $xml =  preg_replace($rgx, $rep, $xml);
    }
    return $xml;
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
altariamx2003Author Commented:
it works

this will be really help me a lot

thanks

P.s. Thanks to take time to answer me
Ray PaseurCommented:
Mr. Alvarez:  Thanks for the points and thanks for using E-E.  Best of luck with the project, ~Ray
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.