Posts Tagged ‘fox-rate’

XML-RPC in PHP :: FoXRate Currency Exchange API

Wednesday, October 21st, 2009

Today my friend Sakib was looking for a Currency Exchange API to implement it on one of his projects. So, I decided to write this small PHP code snippet for him. I hope this will help him and also other developers. Because, XML-RPC section of the PHP Manual is still incomplete :( .

<?php

/*
* Reference :: http://foxrate.org/
*
* For $from and $to :: http://www.oanda.com/site/help/iso_code.shtml
*/

function getExchangeRate($from = 'USD', $to = 'BDT', $ammount = 1.0) {

    $ammount = doubleval($ammount);
    $request = xmlrpc_encode_request("foxrate.currencyConvert", array($from, $to, $ammount));

    $stream = stream_context_create(array('http' => array(
        'method' => "POST",
        'header' => "Content-Type: text/xml\r\nUser-Agent: xmlrpclib.py/1.0  1 (by www.pythonware.com)\r\nHost: foxrate.org\r\n",
        'content' => $request
    )));

    $endpoint = "http://foxrate.org/rpc/";
    $file = file_get_contents($endpoint, false, $stream);
    $response = xmlrpc_decode($file);

    if (is_array($response) && xmlrpc_is_fault($response)) :
        return 'error';
    else :
        return $response;
    endif;
}

print_r(getExchangeRate());

?>