@Mindzai
Sorry for that, I was thinking there is some settings for web server.
So here we go..
I am trying to call http://www.webservicex.net/CurrencyConvertor.asmx web service to convert currency from INR to USD on my localhost. Here everything working fine.
But Once I put it on my apache web server. Here it is not working.
currency_class.php
HTML Code:
<?php
class JOJO_Currency
{
function getCurrencies()
{
return array(
'NZD' => "New Zealand Dollar",
'USD' => "U.S. Dollar",
'EUR' => "Euro",
'GBP' => "British Pound",
'INR' => "Indian Rupee",
);
}
function getRate($from, $to)
{
return false;
}
}
class JOJO_Currency_webservicex extends JOJO_Currency
{
function getRate($from, $to, $snoopy = true)
{
/* Set the $snoopy option to true to avoid any fopen file security issues on some shared hosts */
$url = sprintf("http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency=%s&ToCurrency=%s",
$from,
$to);
if ($snoopy) {
require_once(_PLUGINDIR . "/jojo_convert_currency/external/Snoopy/Snoopy.class.php");
$snoopy = new Snoopy;
$snoopy->fetch($url);
$res = trim(strip_tags($snoopy->results));
} else {
$res = trim(strip_tags(implode('', file($url))));
}
return $res;
}
}
class JOJO_Currency_yahoo extends JOJO_Currency {
function _currency_api_get_desc($currency)
{
foreach ($this->getCurrencies() as $key => $description) {
if ($key == $currency) {
return $description;
}
}
return FALSE;
}
function getRate($currency_from, $currency_to, $snoopy = true)
{
$amount = 1;
$currency_array = array(
's' => 'Currencies',
'l1' => 'Last',
'd1' => 'Date',
't1' => 'Time'
);
$result = array();
$result['status'] = FALSE;
$result['message'] = '';
$result['value'] = '';
$result['date'] = '';
$result['time'] = '';
$from = strtoupper($currency_from);
$to = strtoupper($currency_to);
$url = 'http://finance.yahoo.com/d/quotes.csv?e=.csv&f='. $this->_currency_api_get_fields($currency_array) .'&s='. $from . $to .'=X';
// Validate the passed currency codes, to make sure they are valid
if (FALSE == $this->_currency_api_get_desc($from)) {
$msg = "currency: Invalid currency_from=$from";
$result['message'] = $msg;
$result['status'] = FALSE;
}
if (FALSE == $this->_currency_api_get_desc($currency_to)) {
$msg = "currency: Invalid currency_to=$to";
return FALSE;
$result['message'] = $msg;
$result['status'] = FALSE;
}
if (!is_numeric($amount)) {
$msg = "currency: Invalid amount=$amount";
$result['message'] = $msg;
$result['status'] = FALSE;
}
$handle = @fopen($url, 'r');
if ($handle) {
if ($record = fgets($handle, 4096)) {
fclose($handle);
$currency_data = explode(',', $record);
$rate = $currency_data[1];
$date = $currency_data[2];
$time = $currency_data[3];
// Calculate the result
$value = $amount * $rate;
// Log it
// Got what we need
$result['value'] = $value;
$result['date'] = $date;
$result['time'] = $time;
$result['status'] = TRUE;
$result['message']= 'success';
} else {
$msg = 'currency: fgets failed';
$result['status'] = FALSE;
$result['message'] = $msg;
}
} else {
$msg = 'currency: cannot contact Yahoo Finance host';
$result['status'] = FALSE;
$result['message'] = $msg;
}
return $result['value'];
}
function _currency_api_get_fields($array) {
$field_string = '';
while (list($field, $header) = each($array)) {
$field_string .= $field;
}
return $field_string;
}
}
?>
index.php
HTML Code:
<?php
/************* Exchange Rate Calculator *******************/
/*
Released by AwesomePHP.com, under the GPL License, a
copy of it should be attached to the zip file, or
you can view it on http://AwesomePHP.com/gpl.txt
*/
/************* Exchange Rate Calculator *******************/
// Include Class
require_once('currency_class.php');
// Get list of currencies
$c = new JOJO_Currency_yahoo();
$list = $c->getCurrencies();
// Check any submitions
// Amount to convert
$amount = 2000;
// From
$from = 'INR';
// To
$to = 'USD';
// Get rate
$rate = $c->getRate($from,$to, true);
// Total price (to 2 decemial points)
echo $total = number_format(($rate*$amount),2);
//'hi';
?>
Bookmarks