Configuring a currency converter to get exchange rate feeds for for multiple currencies 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&lt;b&gt;&lt;/b&gt;ression to extrat numeric data
// Split title string to extract currency title 
function get_rate(SimpleXMLElement $xml, $xmlTagPosition) {

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

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

    echo $rate . ' ' . $title . '&lt;br /&gt;';
}


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); 
?&gt;


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-&gt;channel-&gt;item[$x]-&gt;description;
    preg_match('/([0-9]+\.[0-9]+)/', $currency['rate'], $matches);
    $rate = $matches[0];

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

    echo $rate . ' ' . $title . '&lt;br /&gt;';
}
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-&gt;channel-&gt;item[$xmlTagPosition];

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

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

  $title['rate'] = $xml_item-&gt;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' =&gt; array(
    'title' =&gt; '1 British Pound Sterling',
    'currency_id' =&gt; 15
  ),
  'New York' =&gt; array(
    'title' =&gt; '1 US Dollar',
    'currency_id' =&gt; 16
  ),
  'Paris' =&gt; array(
    'title' =&gt; '1 Euro',
    'currency_id' =&gt; 56
  )
);

// now you can easily find and loop through each of your cities
foreach( $cities as $city =&gt; $info ) {
  echo '&lt;p&gt;';
  echo "$city - {$info['title']}";
  echo get_rate($theMoneyConverter, $info['currency_id']);
  echo '&lt;/p&gt;';
}