【问题标题】:Using variables to parse a xml feed with PHP-XPath使用变量通过 PHP-XPath 解析 xml 提要
【发布时间】:2011-01-18 14:37:42
【问题描述】:

我在使用 PHPXPath 根据登录客户端的国家/地区获取汇率时遇到问题。

供参考:PHP XPath 是一个使用 XPath 搜索 XML 文档的 php 类。

我有一个包含所有客户国家和相关货币价值的数据库。

到目前为止,我用来获取利率的代码(来自 ECB 提要)是这样的:

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

现在,我想要传递一个变量作为货币值(上例中的美元)。 我的问题是,因为我对 XPath 完全陌生,所以语法是这样做的。假设变量名是

$user_data->GRUPPO_005

我尝试了以下解决方案,但我不断收到“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"); 

我认为这是因为我对这门语言知之甚少,我希望能有一点提示。

【问题讨论】:

  • 您可能想添加一个指向 PHPXPath 的链接,以便人们知道您在说什么
  • $userRate=$Rates->getAttributes("//Cube[@currency={$user_data->GRUPPO_005}]","rate"); 应该为我工作/工作。我认为这是因为你不能让字符串转义(出于某种原因)。

标签: php variables xpath


【解决方案1】:

好吧,我不知道 PHPXPath 是什么,但既然您似乎无法组装字符串,请尝试

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

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

在旁注中,有一个PEAR Package for the ECB rates,因此您只需使用它就可以省去一些编写自己的查询工具的麻烦。

【讨论】:

  • 非常感谢,一切顺利。另外,PEAR 包装上的提示是天赐之物。
  • @Matteo 不客气。在另一个旁注中,PHP 也有许多非常强大的原生包来处理 XML。查看stackoverflow.com/questions/188414/best-xml-parser-for-php/…
  • 另请注意,变量绑定是 XPath 上下文的一部分,适当的 XPath 引擎应该支持注册它。
  • @Alejandro hmmm, libxml seems to support that 但我不知道如何从任何 PHP 扩展中使用它。
【解决方案2】:

虽然您的问题可能已经解决,但我将为 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){}
    }

...并继续使用您要更新的其他货币。 您甚至可以通过将至少输入过一次的货币的 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'
    }

【讨论】:

    猜你喜欢
    • 2012-06-26
    • 2016-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多