Getting currency conversion data from Yahooapis now that iGoogle is gone

前端 未结 3 1661
孤街浪徒
孤街浪徒 2021-01-24 16:19

Up until yesterday I had a perfectly working budget organizer site/app working with iGoogle.

Through PHP, using the following little line

file_get_conten         


        
相关标签:
3条回答
  • 2021-01-24 16:54

    Still a very useful solution by QuestionerNo27. Since early 2015, however, Yahoo YQL apparently slightly changed the XML output of their api. 'Name' now no longer translates into a string like 'USD to EUR', but to 'USD/EUR' and should in the code above be referenced this way:

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

    instead of

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

    and in a similar fashion for other currency conversions.

    0 讨论(0)
  • 2021-01-24 16:56

    Never mind! Solved it!

    For anyone interested, here's what I did to get my code to work (with the least chnges possible) with the 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 info you want. After that, you use it all something like this:

    amount*$toxxx['coin'];
    

    So, say I wanted to know how many Euro is 100 USD right now:

    100*$toeur['usd'];
    

    Piece of cake!

    0 讨论(0)
  • 2021-01-24 17:07

    I created a routine to convert the currency based on @QuestionerNo27 http://jamhubsoftware.com/geoip/currencyconvertor.php?fromcur=USD&tocur=EUR&amount=1 you can consume 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 讨论(0)
提交回复
热议问题