Receive currency conversion data from Yahooapis now that iGoogle is gone

Until yesterday, I had a great website / budget organizer application working with iGoogle.

Via PHP using the following small line

file_get_contents('http://www.google.com/ig/calculator?hl=en&q=1usd=?eur'); 

and the like, I was able to get everything I needed.

Today it no longer works. When I looked at this issue, it happened that Google resigned iGoogle. What a nuisance!

In any case, I was looking elsewhere, but I can not find anything that would suit my needs. I would REALLY love to just fix it and run it again just by switching this line of code (i.e. by changing the Google address with the address of another available API), but it seems like it doesn't.

The API from rate-exchange.appspot.com seems like it can be an analogue of iGoogle, but, alas, it never works. I keep getting the message "Over Quota".

(This begs the question: does anyone know a simple, reliable iGoogle API?)

So, I believe that it would be natural to use the Yahoo YQL function (at least I believe that it is just as reliable).

Yahoo requests are as follows:

 http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.xchange where pair in ("USDEUR", "USDJPY", "USDBGN")&env=store://datatables.org/alltableswithkeys 

What I really can’t understand is to analyze this data. It outputs XML.

What I used to have is:

 function exchange($inputAmount,$inputCurrency,$outputCurrency) { $exchange = file_get_contents('http://www.google.com/ig/calculator?hl=en&q='.$inputAmount.$inputCurrency.'=?'.$outputCurrency); $exchange = explode('"', $exchange); $exchange = explode('.', $exchange['3']); $exchange[0] = str_replace(" ", "",preg_replace('/\D/', '', $exchange[0])); if(isset($exchange[1])){ $exchange[1] = str_replace(" ", "",preg_replace('/\D/', '', $exchange[1])); $exchange = $exchange[0].".".$exchange[1]; } else{ $exchange = $exchange[0]; } return $exchange; } 

Thus, the user was able to get the exchange rate from the input currency, such as "US dollar", and the output currency, such as "EUR", for a certain amount of money. As I said, this worked until last night.

Any ideas?

+1
source share
3 answers

Nothing! I decided!

For everyone who is interested, here is what I did to get my code working (with the least chances) with Yahoo YQL:

 // ** GET EXCHANGE INFO FROM YAHOO YQL ** // $url = 'http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.xchange where pair in ("USDEUR", "EURUSD")&env=store://datatables.org/alltableswithkeys'; //<-- Get the YQL info from Yahoo (here I'm only interested in converting from USD to EUR and vice-versa; you should add all conversion pairs you need). $xml = simplexml_load_file($url) or die("Exchange feed not loading!"); //<-- Load the XML file into PHP variable. $exchange = array(); //<-- Build an array to hold the data we need. for($i=0; $i<2; $i++): //<-- For loop to get data specific to each exchange pair (you should change 2 to the actual amount of pairs you're querying for). $name = (string)$xml->results->rate[$i]->Name; //<-- Get the name of the pair and turn it into a string (this looks like this: "USD to EUR"). $rate = (string)$xml->results->rate[$i]->Rate; //<-- Do the same for the actual rate resulting from the conversion. $exchange[$name] = $rate; //<-- Put the data pairs into the array. endfor; //<-- End for loop. :) // ** WORK WITH EXCHANGE INFO ** // $toeur = array( //<-- Create new array specific for conversion to one of the units needed. 'usd' => $exchange['USD to EUR'], //<-- Create an array key for each unit used. In this case, in order to get the conversion of USD to EUR I ask for it from my $exchange array with the pair Name. 'eur' => 1); //<-- The way I coded the app, I found it more practical to also create a conversion for the unit into itself and simply use a 1, which translates into "do not convert" $tousd = array( 'eur' => $exchange['EUR to USD'], 'usd' => 1); 

This is basically all you need to get all the exchange information you want. After that you will use all something like this:

 amount*$toxxx['coin']; 

So, tell me, I wanted to know how much the euro is $ 100 right now:

 100*$toeur['usd']; 

Piece of cake!

+2
source

Another very useful QuestionerNo27 solution. However, since the beginning of 2015, Yahoo YQL seems to have slightly changed the XML output of its api. "Name" is no longer translated into a string of the type "USD to EUR", and in "USD / EUR" in this code should be referenced as follows:

 $toeur = array( 'usd' => $exchange['USD/EUR'] 

instead

 $toeur = array( 'usd' => $exchange['USD to EUR'] 

and similarly for other currency conversions.

0
source

I created a currency conversion routine based on @ QuestionerNo27 http://jamhubsoftware.com/geoip/currencyconvertor.php?fromcur=USD&tocur=EUR&amount=1 which you can use to do this

  <?php $fromcur = $_GET['fromcur']; $tocur = $_GET['tocur']; $amt = $_GET['amount']; // ** GET EXCHANGE INFO FROM YAHOO YQL ** // $url = 'http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.xchange where pair in ("'.$fromcur.$tocur.'")&env=store://datatables.org/alltableswithkeys'; //<-- Get the YQL info from Yahoo (here I'm only interested in converting from USD to EUR and vice-versa; you should add all conversion pairs you need). $xml = simplexml_load_file($url) or die("Exchange feed not loading!"); //<-- Load the XML file into PHP variable. $exchange = array(); //<-- Build an array to hold the data we need. for($i=0; $i<2; $i++): //<-- For loop to get data specific to each exchange pair (you should change 2 to the actual amount of pairs you're querying for). $name = (string)$xml->results->rate[$i]->Name; //<-- Get the name of the pair and turn it into a string (this looks like this: "USD to EUR"). $rate = (string)$xml->results->rate[$i]->Rate; //<-- Do the same for the actual rate resulting from the conversion. $exchange[$name] = $rate; //<-- Put the data pairs into the array. endfor; //<-- End for loop. :) // ** WORK WITH EXCHANGE INFO ** // $conv = $fromcur . '/' . $tocur; $toeur = array( //<-- Create new array specific for conversion to one of the units needed. $tocur => $amt*$exchange[$conv], //<-- Create an array key for each unit used. In this case, in order to get the conversion of USD to EUR I ask for it from my $exchange array with the pair Name. $fromcur => $amt, "ex_amt" =>$amt*$exchange[$conv]); //<-- The way I coded the app, I found it more practical to also create a conversion for the unit into itself and simply use a 1, which translates into "do not convert" echo json_encode($toeur); ?> 
0
source

Source: https://habr.com/ru/post/1402045/


All Articles