edainworks.com :: VGR :: webscraping a currency from an XML internet resource.

Run :
running online
opened 'http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml' OK
returned USD currency_value is 1.0830
you can check this by reading the online file http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml
Done.
Code :
$ONLINE=TRUE; //VGR21082009 ADDed
$DEBUG=TRUE; // itou
$remoteuri='http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml'; // itou

function check_currency($currency) {
  GLOBAL $DEBUG, $ONLINE, $remoteuri;
  $currency_value=""; // default value
  if ($ONLINE) $currency_file= fopen($remoteuri,'r');
  else $line=<<<EOS
<?xml version="1.0" encoding="UTF-8"?> <gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref"> <gesmes:subject>Reference rates</gesmes:subject> <gesmes:Sender> <gesmes:name>European Central Bank</gesmes:name> </gesmes:Sender> <Cube> <Cube time='2003-10-03'> <Cube currency='USD' rate='1.1686'/> <Cube currency='JPY' rate='129.29'/> <Cube currency='DKK' rate='7.4287'/> <Cube currency='GBP' rate='0.70020'/> <Cube currency='SEK' rate='8.9883'/> <Cube currency='CHF' rate='1.5440'/> <Cube currency='ISK' rate='89.26'/> <Cube currency='NOK' rate='8.1580'/> <Cube currency='BGN' rate='1.9475'/> <Cube currency='CYP' rate='0.58452'/> <Cube currency='CZK' rate='31.967'/> <Cube currency='EEK' rate='15.6466'/> <Cube currency='HUF' rate='253.81'/> <Cube currency='LTL' rate='3.4524'/> <Cube currency='LVL' rate='0.6495'/> <Cube currency='MTL' rate='0.4284'/> <Cube currency='PLN' rate='4.5426'/> <Cube currency='ROL' rate='38505'/> <Cube currency='SIT' rate='235.6700'/> <Cube currency='SKK' rate='41.225'/> <Cube currency='TRL' rate='1628000'/> <Cube currency='AUD' rate='1.7081'/> <Cube currency='CAD' rate='1.5663'/> <Cube currency='HKD' rate='9.0069'/> <Cube currency='NZD' rate='1.9577'/> <Cube currency='SGD' rate='2.0154'/> <Cube currency='KRW' rate='1342.14'/> <Cube currency='ZAR' rate='8.0104'/> </Cube> </Cube> </gesmes:Envelope>
EOS;

  if ($ONLINE) if ($currency_file===FALSE) die("could not open remote online XML URI '$remoteuri'"); //VGR21082009 ADDed
                else if ($DEBUG) echo "opened '$remoteuri' OK<br>";
  // else continue

  while ( ($ONLINE)?(($line=fgets($currency_file,4096))AND($currency_value=="")):($currency_value=="") ) { // process line just read
    //print $line . "<br>
"; //testPrint
    $line= trim($line) . "
";
    $search="$currency' rate='";
    if (!($a=strpos($line,$search))===FALSE) { // good line found
      $stringok=substr($line,$a+strlen($search));
      $b=strpos($stringok,"'"); // end of rate
      $currency_value=substr($stringok,0,$b); // we will exit the loop
    } // if correct line found
    //print $line . "<br>
";//testPrint
  } // while
  if ($ONLINE) fclose($currency_file);
  return $currency_value; // warning : may return empty string
}
if ($ONLINE) echo "running online<br>"; else echo "using the built-in XML string (emulation)<br>";
$currency_value=check_currency('USD');
echo "returned USD currency_value is $currency_value<br>";
echo "you can check this by reading the online file <a href='$remoteuri' target='_blank'>$remoteuri</a><br>";
echo "Done.";

Vincent Graux (VGR) for European Experts Exchange and Edažn Works  back to list of test scripts
Last update 2024-05-13 15:34:58