Yii Framework Forum: USING GOOGLE API - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

USING GOOGLE API currency converter unresposive Rate Topic: -----

#1 User is offline   Felix27 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 07-May 12

Posted 16 August 2012 - 08:28 PM

Hello
I created a new page withing the framework, and on the page im trying to add a currency converter. I was able to find some code to help create that converter using google api. The code came with two php files, one named ajax_converter.php and the other named booking.php. Everything is handled in the booking.php and includes a javascript in which it calls the ajax_converter. But when running the converter it is just un-responsive. I would like to know if there is anyone who could help me with debugging the code to get the converter to work.
Below is the "booking.php"
<script type="text/javascript">
$(document).ready(function() {

$('#convert').click(function(){

//Get all the values
var amount = $('#amount').val();
var from = $('#fromCurrency').val();
var to = $('#toCurrency').val();

//Make data string
var dataString = "amount=" + amount + "&from=" + from + "&to=" + to;

$.ajax({
type: "POST",
url: "ajax_converter.php",
data: dataString,
success: function(data){
//Show results div
$('#results').show();

//Put received response into result div
$('#results').html(data);
}
});
});
});
</script>

</head>

<body>

<div id="currencyBox">
<div class="data">
<label for="amount">Convert this amount:</label>
<input type="text" name="amount" id="amount" value="1" />
</div>

<div class="data">
<label for="fromCurrency">From this currency:</label>
<select name="fromCurrency" id="fromCurrency">

</select>
</div>

<div class="data">
<label for="toCurrency">To this currency:</label>
<select name="toCurrency" id="toCurrency">

</select>
</div>

<div class="data">
<input type="button" name="convert" id="convert" value="Convert" />
</div>
</div>

<!-- Below conversion results will be displayed -->
<div id="results"></div>
</html>

Here is the ajax_converter.php

<?php
//Get Posted data
$amount = $_POST['amount'];
$from = $_POST['from'];
$to = $_POST['to'];

//make string to be put in API
$string = "1".$from."=?".$to;

//Call Google API
$google_url = "http://www.google.com/ig/calculator?hl=en&q=".$string;

//Get and Store API results into a variable
$result = file_get_contents($google_url);

//Explode result to convert into an array
$result = explode('"', $result);

################################
# Right Hand Side
################################
$converted_amount = explode(' ', $result[3]);
$conversion = $converted_amount[0];
$conversion = $conversion * $amount;
$conversion = round($conversion, 2);

//Get text for converted currency
$rhs_text = ucwords(str_replace($converted_amount[0],"",$result[3]));

//Make right hand side string
$rhs = $conversion.$rhs_text;

################################
# Left Hand Side
################################
$google_lhs = explode(' ', $result[1]);
$from_amount = $google_lhs[0];

//Get text for converted from currency
$from_text = ucwords(str_replace($from_amount,"",$result[1]));

//Make left hand side string
$lhs = $amount." ".$from_text;

################################
# Make the result
################################

echo $lhs." = ".$rhs;exit;
?>
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users