can anyone please tell how to write a simple php code that will convert dollar, euro and yen to pounds. im using a form that will prompt customers to enter a amount they want to convert. thanks in advance

Recommended Answers

All 13 Replies

I have made a use at your own risk function that will convert a number of currencies. The reason why I say use at your own risk is that it uses another website for the conversions. First there is the convert function which is as follows:

function currency_convert($Amount,$currencyfrom,$currencyto)
{
$buffer=file_get_contents('http://finance.yahoo.com/currency-converter');
preg_match_all('/name=(\"|\')conversion-date(\"|\') value=(\"|\')(.*)(\"|\')>/i',$buffer,$match);
$date=preg_replace('/name=(\"|\')conversion-date(\"|\') value=(\"|\')(.*)(\"|\')>/i','$4',$match[0][0]);
unset($buffer);
unset($match);
$buffer=file_get_contents('http://finance.yahoo.com/currency/converter-results/'.$date.'/'.$Amount.'-'.strtolower($currencyfrom).'-to-'.strtolower($currencyto).'.html');
preg_match_all('/<span class=\"converted-result\">(.*)<\/span>/i',$buffer,$match);
$match[0]=preg_replace('/<span class=\"converted-result\">(.*)<\/span>/i','$1',$match[0]);
unset ($buffer);
return $match[0][0];
}

Then there are the different currency codes you can use which are as follows:

-------------
CONVERT FROM:
-------------

British Pound         GBP
Euro                  EUR
US Dollar             USD
Japanese Yen          JPY
Chinese Yuan          CNY
Australian Dollar     AUD
Swiss Franc           CHF
Canadian Dollar       CAD
Thai Baht             THB
Indian Rupee          INR
Indonesian Rupiah     IDR
Hong Kong Dollar      HKD


-----------
CONVERT TO:
-----------

Euro                  EUR
British Pound         GBP
US Dollar             USD
Japanese Yen          JPY
Chinese Yuan          CNY
Australian Dollar     AUD
Swiss Franc           CHF
Canadian Dollar       CAD
Thai Baht             THB
Indian Rupee          INR
Indonesian Rupiah     IDR
Hong Kong Dollar      HKD

And then to use the function you can use the following:

echo currency_convert(150,'EUR','GBP');
commented: Nice reply! +3

Doesn't work when converting from USD

Hi
I've used the lovely bit of script in this thread in my wordpress site, calling up custom field values for the original currency and the numerical value. It means whatever currency the value is given in the initial post custom field, I can get as many different currencies displayed as necessary on displaying the post page.

This is my output code:

<?PHP
$p = get_post_meta($post->ID, 'price', true);
$c = get_post_meta($post->ID, 'currency', true);
echo currency_convert($p,$c,'GBP'); echo currency_convert($p,$c,'EUR'); echo currency_convert($p,$c,'USD');
?>

'price; and 'currency' being my custom fields.

It works well other than the fact it won't output the original currency, simply gives a '0.00' value, which I presume is an error?
Is this a function of your code or the yahoo converter?
Can I cure it?

You can use a currency converter that other sites offer (there are a million) and that will blend well with your site, try find one that is free, and doesn't bother you with massive links to their site. I use this one http://currencycrab.com it looks great on my site and goes well with the style but there are a million others you can choose from, this will reduce your server load too.

Use this code that will work 100%. Try it

<?php
function ConverCurrency($amount,$from_currency,$to_currency)
{
    $string = $amount.strtolower($from_currency)."=?".strtolower($to_currency);
    $google_url = "http://www.google.com/ig/calculator?hl=en&q=".$string;
    $result = file_get_contents($google_url);
    $result = explode('"', $result);
    $confrom = explode(' ', $result[1]);
    $conto = explode(' ', $result[3]);
    return $conto[0];
}
$converted_value = ConverCurrency(1,"usd","eur");
echo $converted_value;
?>

Do i put somthing here:

$result = explode('"', $result);
$confrom = explode(' ', $result[1]);
$conto = explode(' ', $result[3]);

I just get a blank page?

Member Avatar for LastMitch

You need to connect an api feed in order to fill those '"'

OK, how wold i do that. Do you have an example of how that code wold look like?
Thank's

Check this web service with free license. An web service API to get the converted amount and also the relevant currency information. (http://www.ip2currency.com)

$visitorip = $_POST["visitorip"];
$fromcurrencycode = $_POST["fromcurrencycode"];
$tocurrencycode = $_POST["tocurrencycode"];
$fromamount = $_POST["fromamount"];
$license = $_POST["license"]; 

$wsdl = "http://v1.fraudlabs.com/ip2currencywebservice.asmx?wsdl";
$client = new SoapClient($wsdl);
$parms = array("VISITORIP" => $visitorip,"FROMCURRENCYCODE" => $fromcurrencycode,"TOCURRENCYCODE" => $tocurrencycode,"FROMAMOUNT" => $fromamount,"LICENSE" => $license);

$result = $client->IP2Currency($parms);
echo "FROMCURRENCYCODE = " . $result->FROMCURRENCYCODE . "<br>";
echo "TOCURRENCYCODE = " . $result->TOCURRENCYCODE . "<br>";
echo "TOCURRENCYSYMBOL = " . $result->TOCURRENCYSYMBOL . "<br>";
echo "FROMAMOUNT = " . $result->FROMAMOUNT . "<br>";
echo "TOAMOUNT = " . $result->TOAMOUNT . "<br>";
echo "CONVERSIONRATE = " . $result->CONVERSIONRATE . "<br>";
echo "TOCOUNTRYNAME = " . $result->TOCOUNTRYNAME . "<br>";
echo "TOCOUNTRYCODE = " . $result->TOCOUNTRYCODE . "<br>";
echo "CREDITSAVAILABLE = " . $result->CREDITSAVAILABLE . "<br>";
echo "MESSAGE = " . $result->MESSAGE . "<br>";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.