dhimo Posted June 5, 2007 Share Posted June 5, 2007 Hi. Does anyone know any website to get currency exchange rates and integrate it with my website? I want to retrieve exchange rates and update the database if it's possible.Thanks in advance Link to comment Share on other sites More sharing options...
Mr_CHISOL Posted June 5, 2007 Share Posted June 5, 2007 I think it's more "currency sites" out there (use google), but here's one: http://www.xe.com/XE.com offers a free currency converter that you can put on your site, but I guess that's not what you are looking for.They also offer a "Currency Data Feed", which I guess wold suite you, BUT they charge for it ($540 per year), If you are willing to pay (as much), that's an option.You could also use their free currency converter on the site (make your php-script act as a browser sending the POST-data) and then extract the data from the return. You could also extract the data from the table on the first page (if the currencies you want is represented there...)Or you can find a better (free?!) site (please tell if you find one). :?) Link to comment Share on other sites More sharing options...
dhimo Posted June 5, 2007 Author Share Posted June 5, 2007 Heres a good page to get currency exchange rates:http://currencysource.comHeres the path to .xml filehttp://currencysource.com/RSS/USD.xmlwhere USD can be any other currency code. I get rates from .xml file and update the database. Ask if someone needs the code Link to comment Share on other sites More sharing options...
Mr_CHISOL Posted June 5, 2007 Share Posted June 5, 2007 Ok, then it isn't so hard to get the rate (it wouldbe easier if it was "normal XML" and not RSS).Just use SimpleXML (or a lib that let's you handle RSS in an easier way) to get the info you want, the problem is to find the right post...Here's an example, using SimpleXML: $xml = new SimpleXMLElement('http://currencysource.com/RSS/USD.xml', NULL, TRUE);// Loop thru the items...foreach( $xml->channel->item as $item ) { // Check to see if the currency is SEK... if (preg_match('/SEK/', $item->title)) { preg_match( '/(\(.*)\)/', $item->title, $matches ); $rate = $matches[1]; }}// Do something with $rate .../* or like this */// Loop thru the items...foreach( $xml->channel->item as $item ) { // Get the name of the currency and rate... if (preg_match( '/USD = ([A-Z]{3]) (\(.*)\)/', $item->title, $matches )) { $currency = $matches[1]; $rate = $matches[2]; // Do something with currency and rate }} Ok, can't guarranty that those will work correctly, I wrote it (almost) of the top of my head, but it should point you in the right direction... :?) Link to comment Share on other sites More sharing options...
dhimo Posted June 5, 2007 Author Share Posted June 5, 2007 Here's what I did. I have included errors too if it happens $error_message = ""; $success_message = ""; $default_currency = strtoupper(CURR_CODE); $currency_host = "currencysource.com"; $currency_path = "/RSS/" . $default_currency . ".xml"; $currency_url = "http://" . $currency_host . $currency_path; //echo $currency_url; // ===== get rss file with rates $xml_rss = ""; $fp = @fsockopen($currency_host, 80, $errno, $errstr, 5); if ($fp) { fputs($fp, "GET " . $currency_path . " HTTP/1.0\r\n"); fputs($fp, "Host: " . $currency_host . "\r\n"); fputs($fp, "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\n\r\n"); while (!feof($fp)) { $line = fgets($fp); $xml_rss .= $line; } fclose($fp); } else { $error_message = LANG_CURRENCIES_UPDATE_NO_CONNECT.": <strong>" . $currency_host . "</strong>"; return; } // ===== if (!$xml_rss) { $error_message = LANG_CURRENCIES_UPDATE_NO_RESPONSE.": <a href=\"" . $currency_url . "\"><strong>" . $currency_url . "</strong></a>"; return; } elseif (preg_match("/^HTTP\/1\.[01]\s+404/i", trim($xml_rss))) { $error_message = LANG_CURRENCIES_UPDATE_NO_PAGE." <a href=\"" . $currency_url . "\"><strong>" . $currency_url . "</strong></a>"; return; } // ===== $currencies = array(); $sql = mysql_query("SELECT currency_id, currency_code FROM " . TABLE_CURRENCIES . " WHERE currency_default <> 1 OR currency_default IS NULL "); while (list($currency_id, $currcode) = mysql_fetch_array($sql)) { $currency_code = strtoupper($currcode); $currencies[$currency_id] = $currency_code; } // ===== $updated_codes = ""; $error_codes = ""; foreach ($currencies as $currency_id => $currency_code) { if (preg_match ("/\<title\>\s*1\s*" . $default_currency . "\s*=\s*" . $currency_code . "\s*\(([\d\.]+)\)\s*\<\/title\>/i", $xml_rss, $matches) ) { if ($updated_codes) { $updated_codes .= ", "; } $updated_codes .= $currency_code; $exchange_rate = $matches[1]; $sql = " UPDATE " . TABLE_CURRENCIES . " SET exchange_rate = '" . $exchange_rate . "' WHERE currency_id = '" . $currency_id . "'"; mysql_query($sql); } else { if ($error_codes) { $error_codes .= ", "; } $error_codes .= $currency_code; } } if ($updated_codes) { $success_message = LANG_CURRENCIES_UPDATE_SUCCESS_MSG.": <strong>" . $updated_codes . "</strong>"; } if ($error_codes) { $error_message = LANG_CURRENCIES_UPDATE_ERROR_MSG.": <strong>" . $error_codes. "</strong>"; } Link to comment Share on other sites More sharing options...
Mr_CHISOL Posted June 5, 2007 Share Posted June 5, 2007 Well.. I guess that works..................................BUT you have written alot of code that you don't need to write on your own... And I can't get into to my head WHY!!???::SIDE TRACK::I don't want to judge anyone, but this is my opinion: I think the difference between a adanced programmer and a good (advanced) programmer, is that the advanced programmer can handle almost every task, but the qualty of the code isn't the greatest, but a good (advanced) programmer can (also) handle almost every task, but tries to do it as effeiciant (sorry if misspelled) and "gracefully" as possible. I would like to think that I'm the latter, but I'm problably somewhere inbetween...::END OF SIDE TRACK::Ok, back to the topic. The first thing I noticed is that you could use file_get_contents() instead of your 9 rows of code with fsockopen etc..When it comes to the last part, I guess that's the "simplest way" to do it, can think of other ways (with atleast one less query, but don't know if it's faster/more efficiant) Link to comment Share on other sites More sharing options...
dhimo Posted June 7, 2007 Author Share Posted June 7, 2007 Hello. I wouldn't consider myself an advanced programmer. I have been doing this for a year or so, but I am learning and any other suggestions for better coding are highly appreciated, so thank you I will probably use your suggestion. Link to comment Share on other sites More sharing options...
teng84 Posted June 11, 2007 Share Posted June 11, 2007 Maybe what mr. chisol saying is right but when you get in to this field you will learn everything partially the first thing that you will learn basic coding or standard coding afterward everything follows no one lean to do something so fast yet so good so I say practice and learning from mistake makes yougood advance programmer Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.