Search code examples
phphtmlconverterscurrency

Currency converter (user input)


I am searching for a currency converter written in php with a user input, so the user can input a value and then output it. This is the main script.

<?php
    $from_currency = 'EUR';
    $to_currency = 'USD';
    $amount = 1;
    $results = converCurrency($from_currency,$to_currency,$amount);
    $regularExpression = '#\<span class=bld\>(.+?)\<\/span\>#s';
    preg_match($regularExpression, $results, $finalData);
    echo $finalData[0];

    function converCurrency($from,$to,$amount){
        $url = "http://www.google.com/finance/converter?a=$amount&from=$from&to=$to"; 
        $request = curl_init(); 
        $timeOut = 0; 
        curl_setopt ($request, CURLOPT_URL, $url); 
        curl_setopt ($request, CURLOPT_RETURNTRANSFER, 1); 
        curl_setopt ($request, CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"); 
        curl_setopt ($request, CURLOPT_CONNECTTIMEOUT, $timeOut); 
        $response = curl_exec($request); 
        curl_close($request); 
        return $response;
    } 
?>

what do i have to add to this script that the user can put a value of his own.


Solution

  • Google's got some kind of connection limiting on that service, so you'll want to save the results somewhere (as 1 : number) currency units, i.e. 1GBP = 0.65USD