开发者

Using variables to parse a xml feed with PHP-XPath

开发者 https://www.devze.com 2023-02-05 12:28 出处:网络
I have a problem in using PHPXPath to fetch exchange rates based on the country of a logged-in client.

I have a problem in using PHPXPath to fetch exchange rates based on the country of a logged-in client.

For reference: PHP XPath is a php class for searching an XML document using XPath.

I have a database with all the clients nations and associated currency values.

The code I'm using so far to fetch the rates (from ECB feed) is this:

$Rates = new XPath();
$Rates->importFromFile("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml"); 
$userRate = $Rates->getAttributes("//Cube[@currency='USD']","rate"); 

Now, what I want is to pass a variable as the currency value (USD in the example above). My problem is, since I'm completely new to XPath, is the syntax to do that. Assuming the variable name is

$user_data->GRUPPO_005

I've tried the following solutions, but I keep getting "UNEXPECTED T_VARIABLE" error:

$userRate = $Rates->getAttributes("//Cube[@currency='"$user_data->GRUPPO_005"']","rate"); 
$userRate = $Rates->getAttributes("//Cube[@currency='".$user_data->GRUPPO_005."']","rate"); 
$userRate = $Rates->getAttributes("//Cube[@currency='.$user_data->GRUPPO_005.']","rate"); 

I think this i开发者_如何学运维s because of my scarce knowledge of the language, I'd love a small hint of this.


Ok, I have no clue what PHPXPath is but since you seem to have trouble assembling a string, try

$Rates->getAttributes(
    sprintf('//Cube[@currency="%s"]', $user_data->GRUPPO_005),
    'rate'
);

See http://us3.php.net/manual/en/function.sprintf.php

On a sidenote, there is a PEAR Package for the ECB rates, so you could save yourself some trouble writing your own query tool by just using that instead.


While your problem may have been solved, I'll provide some alternate code for ecb.int, just in case you or another user needs to gather the updated rates.

    function getCurrencyRates( $url ){
      $doc = new DOMDocument();
      ini_set('display_errors','Off'); 
      $doc->load( $url );
      ini_set('display_errors','On'); 
      $list = $doc->getElementsByTagName( 'Cube' );

      foreach( $list as $node )
        if( $node->getAttribute( 'currency' ) )
          $rates[
            strtoupper( $node->getAttribute( 'currency' ) )] =
            floatval( $node->getAttribute( 'rate' ) );

      return $rates;
    }

    $url = 'http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml';
    $currencies_array=getCurrencyRates( $url );

    if($currencies_array > ''){
      reset($currencies_array);
    }

    if($currencies_array['USD'] > 0)
    {
      $eurtousd = 1 / $currencies_array['USD'];
    }

    if($eurtousd > 0)
    {

        $sql_update_currencies=mysql_query("UPDATE currencies SET per1usdollar='" . $eurtousd . "', lastupdated=now() WHERE iso='EUR'");

        if($sql_update_currencies){}
    }

... and continue with the other currencies that you are wanting to update. You may even do an automatic loop by grouping the ISO codes for the currencies that have been input at least once.

    $sql_rates_cycle=mysql_query("SELECT iso FROM currencies group BY iso ORDER BY iso ASC");

    while(list($iso)=mysql_fetch_row($sql_rates_cycle))
    {
       //...Put code here similar to that above, 
       //using the variable $iso in place of 'USD'
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消