简体   繁体   English

使用变量通过PHP-XPath解析xml feed

[英]Using variables to parse a xml feed with PHP-XPath

I have a problem in using PHPXPath to fetch exchange rates based on the country of a logged-in client. 我在使用PHPXPath来获取基于已登录客户端所在国家/地区的汇率时遇到问题。

For reference: PHP XPath is a php class for searching an XML document using XPath. 供参考: PHP XPath是用于使用XPath搜索XML文档的php类。

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: 到目前为止,我正在使用的代码(从ECB供稿中)获取费率是:

$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). 现在,我要传递的变量是货币值(在上面的示例中为USD)。 My problem is, since I'm completely new to XPath, is the syntax to do that. 我的问题是,因为我对XPath完全陌生,所以是这样做的语法。 Assuming the variable name is 假设变量名是

$user_data->GRUPPO_005

I've tried the following solutions, but I keep getting "UNEXPECTED T_VARIABLE" error: 我尝试了以下解决方案,但始终收到“ UNEXPECTED T_VARIABLE”错误:

$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 is 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 好的,我不知道PHPXPath是什么,但是由于您似乎在组装字符串时遇到麻烦,请尝试

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

See http://us3.php.net/manual/en/function.sprintf.php 参见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. 在旁注中,有一个针对ECB汇率PEAR软件包 ,因此,您可以使用它代替自己编写查询工具,从而省去了一些麻烦。

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. 虽然您的问题可能已经解决,但我还是为ecb.int提供了一些备用代码,以防万一您或另一个用户需要收集更新的费率。

    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. 您甚至可以通过对至少输入一次的货币的ISO代码进行分组来进行自动循环。

    $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'
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM