user1404623
user1404623

Reputation:

How to get live USD / INR rates in php?

Hi I need to get live USD / INR rates and need to assign it to a field in my sql table. I am not finding a web service for this. Can anybody tell how can I get the live USD to INR rates to be embedded in my php script? I need to do the following thing

$usdinr = some_service_to_retrieve_the_value( USD to INR);

I would appreciate if anyone can tell me how to do this?

Thanks in advance.

Upvotes: 3

Views: 4399

Answers (3)

Clyde Lobo
Clyde Lobo

Reputation: 9174

You can use curl and googles currency converter

<?php

$usdinr = USD2INR("500");

function USD2INR($val) {
$amount = $val;

 $url = 'http://www.google.com/ig/calculator?hl=en&q=' . $amount . 'USD=?INR';
 $ch = curl_init();
 $timeout = 0;
 curl_setopt ($ch, CURLOPT_URL, $url);
 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch,  CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT  6.1)");
 curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
 $rawdata = curl_exec($ch);
 curl_close($ch);
 $data = explode('"', $rawdata);
 $data = explode(' ', $data['3']);
 $var = $data['0'];
 return round($var,3);
 }


?>

EDIT : You can use any of the freely available currency converter API or ones that are listed in the answer. I just used google for the example

Upvotes: 0

Ibu
Ibu

Reputation: 43840

A google search can solve the problem here is what i found

Currency Convertor

It uses SOAP protocol that you can integrate with your php

Upvotes: 0

mittmemo
mittmemo

Reputation: 2090

From this thread:

Get USD to INR exchange rate dynamically in C#?

It looks like you can make a request to

http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency=USD&ToCurrency=INR

Edit:

<?php 
$url  = "http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency=USD&ToCurrency=INR"; 

$xml = simplexml_load_file($url); 

print "<pre>"; 
var_dump($xml); 
print "</pre>"; 
?>

Upvotes: 4

Related Questions