developer tip

프로그래밍 방식으로 환율에 액세스

optionbox 2020. 7. 27. 07:52
반응형

프로그래밍 방식으로 환율에 액세스


온라인 주문 시스템을 설정하고 있지만 호주에 있으며 해외 고객의 경우 미국 달러 나 유로로 가격을 표시하고 싶어서 호주 달러에서 환산하기 위해 정신적으로 노력할 필요가 없습니다.

PHP 스크립트에서 액세스 할 수있는 구문 분석하기 쉬운 형식으로 인터넷에서 최신 환율을 얻을 수 있는지 아는 사람이 있습니까?


업데이트 : 이제 이것을 구현하는 PHP 클래스를 작성했습니다. 당신은 내 웹 사이트에서 코드를 얻을 수 있습니다 .


야후에서 간단한 형식으로 통화 변환을 얻을 수 있습니다.

예를 들어 GBP에서 EUR로 변환하려면 다음을 수행하십시오. http://download.finance.yahoo.com/d/quotes.csv?s=GBPEUR=X&f=sl1d1t1ba&e=.csv


이 답변은 매우 늦었지만 위의 답변에서 누락 된 중요한 정보가 있습니다.

고객에게 정확한 가격을 표시하려면 환율이 어떻게 작동하는지 이해하는 것이 중요합니다.

대부분의 FX 서비스는 스팟 레이트 만 제시합니다 (입찰과 Ask 중간에). 스팟은 환율에 대한 일종의 속기이지만 입찰에서만 판매하거나 요청시 구매할 수 있기 때문에 아무도 스팟을 얻지 못합니다. 일반적으로 둘 사이의 스프레드는 1 % 이상이므로 고객에게는 스팟 속도가 0.5 % 할인됩니다.

그러나 고객이 신용 카드를 사용하고 있으며 Visa / Mastercard / Amex는 모두 외환 수수료를 청구합니다. 이것들은 내 경험상 사소한 것이 아니며, 최소 2.5 %입니다. 예를 들어 Citibank Australia는 3.3 %를 청구합니다. 이는 카드마다 다르므로 고객에게 청구되는 최종 가격을 예측할 수있는 방법이 없습니다.

환율을 기준으로 고객에게 "정확한"가격을 인용하려면 위의 내용을 고려하여 견적보다 더 많은 비용이 청구되지 않도록 버퍼를 제공해야합니다.

FWIW, 나는 F / X 변환이 다르게 나타내는 것에 4 %를 추가했습니다.


추가해도 좋을지도 모른다

  http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml

목록에.

공식 참조 요금은 유럽 ​​중앙 은행 내외부 중앙 은행 간의 정기적 인 일일 콘서트 절차에 따라 유럽 중앙 은행이 제공합니다.

피드는 XML 및 기타 형식으로되어 있습니다 .
업데이트는 일반적으로 오후 2시 15 분 (14:15) ECB 시간 (= 프랑크푸르트 시간)에 이루어집니다.


최근에 같은 것을 구현했지만 Google의 API를 사용했습니다. 쿼리 URL은 다음과 같습니다.

http://www.google.com/ig/calculator?hl=en&q=1GBP=?USD

3 개의 매개 변수가 필요합니다. 첫 번째 매개 변수는 금액이며 그 뒤에는 변환 하는 ISO 4217 통화 코드, 등호 및 물음표 및 변환하는 통화 코드가옵니다. 여기에서 Google이 지원하는 코드 목록을 찾을 수 있습니다 . 쿼리에 대한 응답은 다음과 같습니다.

{lhs: "1 British pound",rhs: "1.6132 U.S. dollars",error: "",icc: true}

이것은 매우 자명 한 설명이므로 여기서는 자세히 설명하지 않겠습니다. 이것이 쿼리 응답을 처리하는 방법입니다.

function convert_currency($amount, $from_code, $to_code){
    ini_set('max_execution_time', 60);
    $temp = 'http://www.google.com/ig/calculator?hl=en&q=' . $amount . $from_code . '=?' . $to_code;

    $response = file_get_contents($temp);
    $result_string = explode('"', $response);

    $final_result = $result_string['3'];

    $float_result = preg_replace("/[^0-9\.]/", '', $full_result);

    return $float_result;
}

이 작업을 수행하는 가장 우아한 방법과는 거리가 멀지 만 PHP를 처음 사용합니다. 그것이 도움이되기를 바랍니다!


또 다른 매우 훌륭한 무료 및 오픈 소스 링크는 다음과 같습니다.

https://raw.github.com/currencybot/open-exchange-rates/master/latest.json
(I found about it here: http://josscrowcroft.github.com/open-exchange-rates/)

[Update]:
Open Exchange Rates project data has been moved away from GitHub.
It is available now at: http://openexchangerates.org/
Data in JSON format is available at: http://openexchangerates.org/latest.json

No access fees, no rate limits, No ugly XML - just free, hourly updated exchange rates in JSON format.
This is not "entirely" free now. The new licensing states that up to 1000 hits per month is allowed, and then you need to pay. You also need to pay if you want to use the single currency converter (basic functionality).

[ Note: You may want to look at this answer as well. ]


I added Open Data table to YQL, you can use it to retrieve exchange rate data from yahoo.finance.

Try it in YQL console

Comma-separated format is preferrable over "where pair in ('EURUSD','GBPUSD')" but anyway, you can use both and even intermix them.


Here is a Soap service that offers exchange rate

http://www.newyorkfed.org/markets/pilotfx.html


This site has a currency converter service for free:

http://www.webservicex.net/WS/WSDetails.aspx?WSID=10


Try this RESTful (I'm not sure if this is really a REST, since i got this originally from a SOAP, I just tried to access it using HTTP GET)


iGoogle was retired on November 1, 2013. This API no longer works.

To get the exchange rate you can use something like this:

function get_exchange_rate($from, $to){
    $data = file_get_contents("http://www.google.com/ig/calculator?hl=en&q=1{$from}=?{$to}");
    preg_match('/rhs\:\s?"([0-9\.]+)/', $data, $m);
    return $m[1];
}

You could add a DB cache in there to make sure you don't get throttled etc.

As has been noted on other posts / comments you'd then use this rate to calculate your currencies


XE.com provides feed for their exchange rates. Not free though.


Oanda.com exposes currency rates as an XML API, but not for free


coinnill.com has a sort-of web-service.

http://coinmill.com/rss/AUD_USD.xml

will give you the AUD --> USD rate for example. You'll just need to parse the XML that comes back.


I feel compelled to add:

http://www.exchangerate-api.com/

Dead simple to use with a clean RESTful API and signup takes 5 seconds. Includes coding examples for most major languages, most are 2-3 lines long.

Rates are updated hourly, so it's fine for most uses, and you can get 30000 monthly queries for $7 a month. I've never needed more than that, but the rates are very reasonable for higher volumes.


This is working for me .

A currency exchange rate API : http://currency-api.appspot.com/

참고URL : https://stackoverflow.com/questions/181990/programmatically-access-currency-exchange-rates

반응형