使用变量用PHP-XPath解析xml提要

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

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

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

到目前为止我用来获取的代码 费率(来自ECB Feed)是这样的:</ p>

  $ Rates = new XPath(); 
$ Rates-&gt; importFromFile(“http://www.ecb。 INT /统计/ eurofxref / eurofxref-daily.xml“);

$ userRate = $ Rates-&gt; getAttributes(“// Cube [@ currency ='USD']”,“rate”);

</ code> </ pre>

现在,我想要的是传递一个变量作为货币值(上例中的美元)。
我的问题是,因为我是全新的 到XPath,是这样做的语法。 假设变量名是</ p>

  $ user_data-&gt; GRUPPO_005 
</ code> </ pre>

我尝试了以下解决方案, 但我一直收到“意外的T_VARIABLE”错误:</ p>

  $ userRate = $ Rates-&gt; getAttributes(“// Cube [@currency ='”$ user_data-&gt; GRUPPO_005  “']“,“率”);

$ userRate = $ Rates-&gt; getAttributes(“// Cube [@currency ='”。$ user_data-&gt; GRUPPO_005。“']”,“rate”);

$ userRate = $ Rates-&gt; getAttributes(“// Cube [@currency ='。$ user_data-&gt; GRUPPO_005。']”,“rate”);

</ code> </ pre>

我认为这是因为我对语言的了解很少,我会喜欢这一点。</ p>
</ div>

展开原文

原文

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 is because of my scarce knowledge of the language, I'd love a small hint of this.

dqqs64238
dqqs64238 $userRate=$汇率─>的getAttributes(“//立方[@currency={$user_data->GRUPPO_005}]”,“速率”);应该为我工作/工作。我认为这是因为你不能将字符串转义(出于某种原因)。
接近 9 年之前 回复
dragon8002
dragon8002 您可能想要添加一个指向PHPXPath的链接,以便人们知道您在说什么
接近 10 年之前 回复

2个回答



好的,我不知道PHPXPath是什么,但是因为你似乎在组装字符串时遇到了麻烦,试试</ p>
\ n

  $ Rates-&gt; getAttributes(
sprintf('// Cube [@currency =“%s”]',$ user_data-&gt; GRUPPO_005),
'rate'
);

</ code> </ pre>

请参阅 http ://us3.php.net/manual/en/function.sprintf.php </ p>

在旁注中,有一个 PEAR包的ECB费率,所以你可以通过改用它来自己编写自己的查询工具。</ p>
</ DIV>

展开原文

原文

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.

dqrq93879
dqrq93879 嗯,libxml似乎支持,但我不知道如何使用任何PHP扩展。
接近 10 年之前 回复
drasebt1835
drasebt1835 另请注意,变量绑定是XPath上下文的一部分,正确的XPath引擎应该支持注册它。
接近 10 年之前 回复
dongpu2727
dongpu2727 别客气。 在另一个旁注中,PHP还有许多非常强大的本机包来处理XML。 查看stackoverflow.com/questions/188414/best-xml-parser-for-php / ...
接近 10 年之前 回复
drhzc64482
drhzc64482 too long
接近 10 年之前 回复



虽然您的问题可能已经解决了,但我会为ecb.int提供一些备用代码,以防您或其他用户需要 收集更新的费率。</ p>

  function getCurrencyRates($ url){
$ doc = new DOMDocument();
ini_set('display_errors','Off');

$ doc-&gt; load($ url);
ini_set('display_errors','On');

$ list = $ doc-&gt; getElementsByTagName('Cube');

foreach($ list as $ node)
if($ node-&gt; getAttribute('currency'))
$ rates [

strtoupper($ node-&gt; getAttribute('currency'))] =
floatval($ node-&gt; getAttribute('rate'));

返回$ rates;
}

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

if if($ currency_array&gt;''){

重置($ currency_array);
}

if($ currency_array ['USD']&gt; 0)
{
$ eurtousd = 1 / $ currency_array ['USD'];
} \ n
if($ eurtousd&gt; 0)
{

$ sql_update_currencies = mysql_query(“UPDATE currency SET per1usdollar ='”。$ eurtousd。“',lastupdated = now()WHERE iso ='EUR'” );

if if($ sql_update_currencies){}
}
</ code> </ pre>

...并继续使用您想要更新的其他货币。\ 你甚至可以通过对ISO公司进行分组来进行自动循环 des至已输入至少一次的货币。</ p>

  $ sql_rates_cycle = mysql_query(“SELECT iso FROM currency group BY iso ORDER BY iso ASC”); 

while(list($ iso)= mysql_fetch_row($ sql_rates_cycle))
{
//...Put代码与此类似,
//使用变量$ iso代替'USD'
}

</ code> </ pre>
</ div>

展开原文

原文

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐