Category: PHP


It’s long since I last wrote about technology use.

I am not a geek on myself but would like to share about my little idea to retreat currency rate from different country via web service using PHP and plug to javascript compiling with the helping hand of mighty j-Query.

Pre-requisites:
1.) I assumed you mater PHP, I assume you master below all.
2.) I have some syntax error and my code are unstructured, if you’re curiosity could kill a cat, try explore more yourself, ya; I learned that well and until today too.

What you need doing above:
1.) Internet connection of course.
2.) Apache, php with nusoap.dll enabled if I not mistaken the dll name :P .
3.) javascript jQuery copy no matter what version.
4.) Price list covered by div / span or any wrapper.
5.) a url http://www.webservicex.net/CurrencyConvertor.asmx?wsdl

Steps:
1.) Do your PHP nusoap to post variable to the 5.) url. Or you can just copy my code below:

#name this code get-currency.php
#for a list of currency code refer to this url: http://www.webservicex.net/WCF/ServiceDetails.aspx?SID=18
#thanks to W.Lai for this code contributions
$from = ‘MYR’;$to = ‘USD’;$sUrl = ‘http://www.webservicex.net/CurrencyConvertor.asmx?WSDL’;$client = new SoapClient($sUrl, array(‘trace’ => 1));$a = $client->ConversionRate(array(“FromCurrency” => $from, “ToCurrency” => $to));$johnny = $a->ConversionRateResult;print $johnny;

2.) for example now you can retreat this USD currency rate,  you can write a sample html to retreat like below:
/*partial of your html code like below*/
<div id=”Price1″ rel=”230.00″>230.00</div><div id=”Price3″ rel=”4500.00″>4500.00</div><select id=”currencyBox”><option value=”">Select your currency</option><option value=”USD”/>US Dollar<option value=”CNY”/>Chinese Yuen</select></html>


3.) in your javascript of course you first call jQuery  file, then you configure jQuery  regular expression selector to convert their currency using jQuery base AJAX $.get like below:
<script type=”text/javascript”>
$(document).ready(function(){
$(‘#currencBx’).change(function(){
var val = $this.value;
$(“div[id^=Prce]“).each(function(i){$(this).html(‘<i>Loading ‘+val+’ currency rate..</i>’;});
$.get(‘get-currency.php?from=MYR&to=’+val,{}, function(data, textStatus){if(textStatus==’success’){$(“div[id^=Prce]“).each(function(i){$(this).html(parseFloat(data) * parseFloat($(this).attr(‘rel’)); });}});});});</script>

4.) Done, then when you use the select box and change the value, it will grab the currency from webservicex and compile the new exchange rate to the screen without refershing, just by this simple code.

$sUrl = ‘http://www.webservicex.net/CurrencyConvertor.asmx?WSDL’;
$client = new SoapClient($sUrl, array(‘trace’ => 1));
$a = $client->ConversionRate(array(“FromCurrency” => $from, “ToCurrency” => $to));
$_SESSION['D_CURRENCY'] = $to;
$_SESSION['rate'] = $a->ConversionRateResult;
Follow

Get every new post delivered to your Inbox.