3

次のコード (以下) があり、iGoogle バージョンを使用していました。

   $url = 'http://www.google.com/ig/calculator?hl=en&q=' . $amount . $from_Currency . '=?' . $to_Currency;

    $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];

しかし、別の URL を使用しているように見えます。

  'http://www.google.com/finance/converter?hl=en&a=' . $amount . '&from=' . $from_Currency . '&to=USD';

しかし、単に URL を変更しただけでは、慣れ親しんだ必要な結​​果が返されません。

今私が得るすべては

 http://www.w3.org/TR/html4/strict.dtd

SO には、この最新の通貨コンバーターの URL に取り組んだ人や、アイデアがある人がいます。または、PHP を使用した代替

4

4 に答える 4

4

XPath の使用:

function currency($from, $to, $amount)
{
   $content = file_get_contents('https://www.google.com/finance/converter?a='.$amount.'&from='.$from.'&to='.$to);

   $doc = new DOMDocument;
   @$doc->loadHTML($content);
   $xpath = new DOMXpath($doc);

   $result = $xpath->query('//*[@id="currency_converter_result"]/span')->item(0)->nodeValue;

   return str_replace(' '.$to, '', $result);
}

echo currency('USD', 'EUR', 1); // returns 0.7216
于 2014-05-02T10:53:29.780 に答える
2

ここで、Google に簡単に接続するためのクラスを作成しました。

通貨コンバーター php

いくつかの側面が簡単になることを願っています!

編集:Google サービスが 2013 年 11 月に閉鎖されたことを知りました。

私はそれを変更する必要があります !

もう一度編集: Google Api を Yahoo Api に変更しましたが、完全に正常に動作します!

于 2013-11-07T14:57:14.650 に答える
1

私は別の解決策を見つけました。サーバー IP が Google サービスを使用できない場合にも機能します。

<?php
    $from_currency    = 'USD';
    $to_currency    = 'INR';
    $amount            = 1;
    $results = converCurrency($from_currency,$to_currency,$amount);
    $regularExpression     = '#\<span class=bld\>(.+?)\<\/span\>#s';
    preg_match($regularExpression, $results, $finalData);
    echo $finalData[0];

    function converCurrency($from,$to,$amount){
        $url = "http://www.google.com/finance/converter?a=$amount&from=$from&to=$to"; 
        $request = curl_init(); 
        $timeOut = 0; 
        curl_setopt ($request, CURLOPT_URL, $url); 
        curl_setopt ($request, CURLOPT_RETURNTRANSFER, 1); 
        curl_setopt ($request, CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"); 
        curl_setopt ($request, CURLOPT_CONNECTTIMEOUT, $timeOut); 
        $response = curl_exec($request); 
        curl_close($request); 
        return $response;
    } 
?>

ソース参照Step Blogging

于 2016-06-29T14:56:59.657 に答える