3
\$\begingroup\$

I'm trying to make my function more versatile by removing hard coded data.

function get_currency_rate($currencySource) {

if (isset($currencySource)) {
        $feed = $currencySource;
    } else {
        echo 'Feed not found.  Check URL';
    }

    if (!$feed) {
        echo('Feed not found');
    }


    $xml = new SimpleXmlElement($feed);

    $rate = get_rate($xml, 15); //EUR
    if ($feed == 'http://themoneyconverter.com/rss-feed/USD/rss.xml') {
        $rate = get_rate($xml, 16); //GBP
    } else {
        $rate = get_rate($xml, 56);  //USD
    }
}

I would prefer to pass in the values 15, 16, 56 and the URL 'http://themoneyconverter.com/rss-feed/USD/rss.xml' rather than hard coding them into the function.

Below is the get_rate function.

// Get and return currency rate
// Perform regular exp<b></b>ression to extrat numeric data
// Split title string to extract currency title 
function get_rate(SimpleXMLElement $xml, $xmlTagPosition) {

    $currency['rate'] = $xml->channel->item[$xmlTagPosition]->description;
    preg_match('/([0-9]+\.[0-9]+)/', $currency['rate'], $matches);
    $rate = $matches[0];

    $title['rate'] = $xml->channel->item[$xmlTagPosition]->title;
    $title = explode('/', $title['rate']);
    $title = $title[0];

    echo $rate . ' ' . $title . '<br />';
}

The feed URLs are set in my cityConfig.php script as below:

// Feed URLs //
$theMoneyConverter = 'http://themoneyconverter.com/rss-feed/';

// Define arrays // 
$cities = array('London', 'New York', 'Paris');
$currencyHeadings = array('London - 1 British Pound Sterling', 'New York - 1 US Dollar', 'Paris - 1 Euro');
$currencySource = array($theMoneyConverter . 'GBP/rss.xml', $theMoneyConverter . 'USD/rss.xml', $theMoneyConverter . 'EUR/rss.xml');
$currencyId = array(15, 16, 56); 
?>

Notice the $currencyId array values are the same as those hard coded into the get_currency_rate function. I tried passing in a second parameter for the $currencyId to no avail. Any ideas where I'm going wrong?

I have tried the following:

Added an argument onto the end of the feed URL's e.g.

?x=15 // for EURO feed
?x=16 // for GBP feed
?x=56 // for USD feed

These numbers correspond to the position of the XML tag as an array from the feed URL's.

$currencySource = array($theMoneyConverter . 'GBP/rss.xml?x=15', $theMoneyConverter . 'USD/rss.xml?x=16', $theMoneyConverter . 'EUR/rss.xml?x=56');

I have edited the get_rate function to parse in the URL and the PHP query.

// Get and return currency rate
// Perform regular expression to extrat numeric data
// Split title string to extract currency title 
function get_rate(SimpleXMLElement $xml) {

    $vars = parse_url($xml, PHP_URL_QUERY);
    parse_str($vars);
    echo "x = " . $x;
    //$x = 15; 

    $currency['rate'] = $xml->channel->item[$x]->description;
    preg_match('/([0-9]+\.[0-9]+)/', $currency['rate'], $matches);
    $rate = $matches[0];

    $title['rate'] = $xml->channel->item[$x]->title;
    $title = explode('/', $title['rate']);
    $title = $title[0];

    echo $rate . ' ' . $title . '<br />';
}
\$\endgroup\$
1
\$\begingroup\$

Well the first thing I notice is that your get_currency_rate() function requires a $currencySource to be added as a parameter. Which makes the first if statement a little redundant. The other thing is that get_currency_rate() is setting the variable $rate from get_rate(), but get_rate() doen't return anything.

I think you should ommit the get_currency_rate() function and change get_rate() to handle the XML feed request and return the information you want rather than using two separate functions.

It would look like this.

function get_rate($feed_url, $xmlTagPosition) {
  $xml = new SimpleXmlElement($feed_url);
  $xml_item = $xml->channel->item[$xmlTagPosition];

  // make sure the $xmlTagPosition was valid
  if( empty($xml_item) )
    return false;

  $currency['rate'] = $xml_item->description;
  preg_match('/([0-9]+\.[0-9]+)/', $currency['rate'], $matches);
  $rate = $matches[0];

  $title['rate'] = $xml_item->title;
  $title = explode('/', $title['rate']);
  $title = $title[0];

  return $rate . ' ' . $title;
}

And now your cityConfig.php file can work more like this.

$theMoneyConverter = 'http://themoneyconverter.com/rss-feed/USD/rss.xml';

$cities = array(
  'London' => array(
    'title' => '1 British Pound Sterling',
    'currency_id' => 15
  ),
  'New York' => array(
    'title' => '1 US Dollar',
    'currency_id' => 16
  ),
  'Paris' => array(
    'title' => '1 Euro',
    'currency_id' => 56
  )
);

// now you can easily find and loop through each of your cities
foreach( $cities as $city => $info ) {
  echo '<p>';
  echo "$city - {$info['title']}";
  echo get_rate($theMoneyConverter, $info['currency_id']);
  echo '</p>';
}
\$\endgroup\$
  • 1
    \$\begingroup\$ You might want to look into using a direct API for currency conversion (dynamicguru.com/php/…) That way you don't have to worry about the xml tag position changing. \$\endgroup\$ – Baylor Rae' Feb 8 '12 at 20:09
1
\$\begingroup\$

I would start by introducing a few variables, and a sub to set them.

function set_values(&$initial, &$matched, &$unmatched, &$matchFeed){
  $initial =15;  //eur
  $matched=16;   //GBP
  $unmatched=56; //USD
  $matchFeed = "http://themoneyconverter.com/rss-feed/USD/rss.xml";

}

function get_currency_rate($currencySource) {
    if (isset($currencySource)) {
        $feed = $currencySource;
    } else {
        echo 'Feed not found.  Check URL';
    }

    if (!$feed) {
        echo('Feed not found');
    }

    $xml = new SimpleXmlElement($feed);

    set_values($initital, $matched, $unmatched, $matchFeed);

    $rate = get_rate($xml, $initial);
    if ($feed == $matchFeed) {
        $rate = get_rate($xml, $matched);
    } else {
        $rate = get_rate($xml, $unmatched);
    }
}

Now you are ready to get the values from wherever you want to, without changing the primary function.

Once that is done, you might want to take a look at the config.php file.

$currencyIds= array(15, 16, 56); 
$currencySource = array($theMoneyConverter . 'GBP/rss.xml', $theMoneyConverter . 'USD/rss.xml', $theMoneyConverter . 'EUR/rss.xml', $currencyIds );

Creates an array wih 3 strings and one array of three integers(four total). Depending upon your usage, that might be what you want.

?>

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.