1

昨日まで、私は iGoogle で動作する完全に機能する予算オーガナイザー サイト/アプリを持っていました。

PHP を介して、次の小さな行を使用して

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

同様に、必要なものはすべて手に入れることができました。

今日の時点で、これは機能しなくなりました。問題を調べたところ、Google が iGoogle を廃止したということです。残念!

とにかく、私は他の場所を探していましたが、自分のニーズに合ったものを見つけることができません. この1行のコードを切り替えるだけで(つまり、Googleアドレスを利用可能な他の通貨APIのアドレスに変更するだけで)修正して再び実行できるようになりたいと思っていますが、そうではないようです。

rate-exchange.appspot.com の API は、iGoogle のアナログのように見えますが、残念ながら、まったく機能しません。「オーバー クォータ」メッセージが表示され続けます。

(最初の質問です。シンプルで信頼性の高い iGoogle ソート API を知っている人はいますか?)

したがって、自然なことは Yahoo YQL 機能になると思います (少なくとも、信頼性は高いと思います)。

Yahoo のクエリは次のようになります。

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

私が本当に理解できないのは、このデータを解析する方法です。XML を出力します。

私が以前持っていたのはこれです:

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;
}

そのため、ユーザーは、特定の金額について、「USD」などの入力通貨と「EUR」などの出力通貨から為替レートを取得できました。私が言ったように、これは昨日の夜まで問題なく動作していました。

何か案は?

4

3 に答える 3

0

QuestionerNo27 による非常に便利なソリューションです。しかし、2015 年初頭以降、Yahoo YQL は API の XML 出力をわずかに変更したようです。「名前」は「USD to EUR」のような文字列に変換されなくなり、「USD/EUR」に変換され、上記のコードでは次のように参照される必要があります。

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

それ以外の

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

他の通貨換算についても同様です。

于 2015-10-12T08:45:19.320 に答える
0

@QuestionerNo27 に基づいて通貨を変換するルーチンを作成しました http://jamhubsoftware.com/geoip/currencyconvertor.php?fromcur=USD&tocur=EUR&amount=1 これを消費できます

    <?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);

?>
于 2016-02-04T07:34:14.267 に答える