Convert currency using google finance calculator in php

Earlier we used to Convert currency using Google api. But as Google has closed its iGoogle service from 1st Nov 2013, this api will no longer work. But we have an alternative to convert currency using google finance calculator.


Here is a little code snippet that can be used to convert currency from one format to another.
It takes three parameters The amount to be converted, convert from and convert to

  <?php
  function convertCurrency($amount, $from, $to){
    $url  = "https://finance.google.com/finance/converter?a=$amount&from=$from&to=$to";
    $data = file_get_contents($url);
    preg_match("/<span class=bld>(.*)<\/span>/",$data, $converted);
    $converted = preg_replace("/[^0-9.]/", "", $converted[1]);
    return round($converted, 3);
  }

  # Call function  
  echo convertCurrency(1, "USD", "INR");
  ?>

The above php function returns something like 62.285

The Currency formats can be of following types


    AED => United Arab Emirates Dirham (AED)
    AFN => Afghan Afghani (AFN)
    ALL => Albanian Lek (ALL)
    AMD => Armenian Dram (AMD)
    ANG => Netherlands Antillean Guilder (ANG)
    AOA => Angolan Kwanza (AOA)
    ARS => Argentine Peso (ARS)
    AUD => Australian Dollar (A$)
    AWG => Aruban Florin (AWG)
    AZN => Azerbaijani Manat (AZN)
    BAM => Bosnia-Herzegovina Convertible Mark (BAM)
    BBD => Barbadian Dollar (BBD)
    BDT => Bangladeshi Taka (BDT)
    BGN => Bulgarian Lev (BGN)
    BHD => Bahraini Dinar (BHD)
    BIF => Burundian Franc (BIF)
    BMD => Bermudan Dollar (BMD)
    BND => Brunei Dollar (BND)
    BOB => Bolivian Boliviano (BOB)
    BRL => Brazilian Real (R$)
    BSD => Bahamian Dollar (BSD)
    BTN => Bhutanese Ngultrum (BTN)
    BWP => Botswanan Pula (BWP)
    BYR => Belarusian Ruble (BYR)
    BZD => Belize Dollar (BZD)
    CAD => Canadian Dollar (CA$)
    CDF => Congolese Franc (CDF)
    CHF => Swiss Franc (CHF)
    CLF => Chilean Unit of Account (UF) (CLF)
    CLP => Chilean Peso (CLP)
    CNH => CNH (CNH)
    CNY => Chinese Yuan (CN¥)
    COP => Colombian Peso (COP)
    CRC => Costa Rican Colón (CRC)
    CUP => Cuban Peso (CUP)
    CVE => Cape Verdean Escudo (CVE)
    CZK => Czech Republic Koruna (CZK)
    DEM => German Mark (DEM)
    DJF => Djiboutian Franc (DJF)
    DKK => Danish Krone (DKK)
    DOP => Dominican Peso (DOP)
    DZD => Algerian Dinar (DZD)
    EGP => Egyptian Pound (EGP)
    ERN => Eritrean Nakfa (ERN)
    ETB => Ethiopian Birr (ETB)
    EUR => Euro (€)
    FIM => Finnish Markka (FIM)
    FJD => Fijian Dollar (FJD)
    FKP => Falkland Islands Pound (FKP)
    FRF => French Franc (FRF)
    GBP => British Pound Sterling (£)
    GEL => Georgian Lari (GEL)
    GHS => Ghanaian Cedi (GHS)
    GIP => Gibraltar Pound (GIP)
    GMD => Gambian Dalasi (GMD)
    GNF => Guinean Franc (GNF)
    GTQ => Guatemalan Quetzal (GTQ)
    GYD => Guyanaese Dollar (GYD)
    HKD => Hong Kong Dollar (HK$)
    HNL => Honduran Lempira (HNL)
    HRK => Croatian Kuna (HRK)
    HTG => Haitian Gourde (HTG)
    HUF => Hungarian Forint (HUF)
    IDR => Indonesian Rupiah (IDR)
    IEP => Irish Pound (IEP)
    ILS => Israeli New Sheqel (₪)
    INR => Indian Rupee (Rs.)
    IQD => Iraqi Dinar (IQD)
    IRR => Iranian Rial (IRR)
    ISK => Icelandic Króna (ISK)
    ITL => Italian Lira (ITL)
    JMD => Jamaican Dollar (JMD)
    JOD => Jordanian Dinar (JOD)
    JPY => Japanese Yen (¥)
    KES => Kenyan Shilling (KES)
    KGS => Kyrgystani Som (KGS)
    KHR => Cambodian Riel (KHR)
    KMF => Comorian Franc (KMF)
    KPW => North Korean Won (KPW)
    KRW => South Korean Won (₩)
    KWD => Kuwaiti Dinar (KWD)
    KYD => Cayman Islands Dollar (KYD)
    KZT => Kazakhstani Tenge (KZT)
    LAK => Laotian Kip (LAK)
    LBP => Lebanese Pound (LBP)
    LKR => Sri Lankan Rupee (LKR)
    LRD => Liberian Dollar (LRD)
    LSL => Lesotho Loti (LSL)
    LTL => Lithuanian Litas (LTL)
    LVL => Latvian Lats (LVL)
    LYD => Libyan Dinar (LYD)
    MAD => Moroccan Dirham (MAD)
    MDL => Moldovan Leu (MDL)
    MGA => Malagasy Ariary (MGA)
    MKD => Macedonian Denar (MKD)
    MMK => Myanma Kyat (MMK)
    MNT => Mongolian Tugrik (MNT)
    MOP => Macanese Pataca (MOP)
    MRO => Mauritanian Ouguiya (MRO)
    MUR => Mauritian Rupee (MUR)
    MVR => Maldivian Rufiyaa (MVR)
    MWK => Malawian Kwacha (MWK)
    MXN => Mexican Peso (MX$)
    MYR => Malaysian Ringgit (MYR)
    MZN => Mozambican Metical (MZN)
    NAD => Namibian Dollar (NAD)
    NGN => Nigerian Naira (NGN)
    NIO => Nicaraguan Córdoba (NIO)
    NOK => Norwegian Krone (NOK)
    NPR => Nepalese Rupee (NPR)
    NZD => New Zealand Dollar (NZ$)
    OMR => Omani Rial (OMR)
    PAB => Panamanian Balboa (PAB)
    PEN => Peruvian Nuevo Sol (PEN)
    PGK => Papua New Guinean Kina (PGK)
    PHP => Philippine Peso (Php)
    PKG => PKG (PKG)
    PKR => Pakistani Rupee (PKR)
    PLN => Polish Zloty (PLN)
    PYG => Paraguayan Guarani (PYG)
    QAR => Qatari Rial (QAR)
    RON => Romanian Leu (RON)
    RSD => Serbian Dinar (RSD)
    RUB => Russian Ruble (RUB)
    RWF => Rwandan Franc (RWF)
    SAR => Saudi Riyal (SAR)
    SBD => Solomon Islands Dollar (SBD)
    SCR => Seychellois Rupee (SCR)
    SDG => Sudanese Pound (SDG)
    SEK => Swedish Krona (SEK)
    SGD => Singapore Dollar (SGD)
    SHP => Saint Helena Pound (SHP)
    SLL => Sierra Leonean Leone (SLL)
    SOS => Somali Shilling (SOS)
    SRD => Surinamese Dollar (SRD)
    STD => São Tomé and Príncipe Dobra (STD)
    SVC => Salvadoran Colón (SVC)
    SYP => Syrian Pound (SYP)
    SZL => Swazi Lilangeni (SZL)
    THB => Thai Baht (฿)
    TJS => Tajikistani Somoni (TJS)
    TMT => Turkmenistani Manat (TMT)
    TND => Tunisian Dinar (TND)
    TOP => Tongan Paʻanga (TOP)
    TRY => Turkish Lira (TRY)
    TTD => Trinidad and Tobago Dollar (TTD)
    TWD => New Taiwan Dollar (NT$)
    TZS => Tanzanian Shilling (TZS)
    UAH => Ukrainian Hryvnia (UAH)
    UGX => Ugandan Shilling (UGX)
    USD => US Dollar ($)
    UYU => Uruguayan Peso (UYU)
    UZS => Uzbekistan Som (UZS)
    VEF => Venezuelan Bolívar (VEF)
    VND => Vietnamese Dong (₫)
    VUV => Vanuatu Vatu (VUV)
    WST => Samoan Tala (WST)
    XAF => CFA Franc BEAC (FCFA)
    XCD => East Caribbean Dollar (EC$)
    XDR => Special Drawing Rights (XDR)
    XOF => CFA Franc BCEAO (CFA)
    XPF => CFP Franc (CFPF)
    YER => Yemeni Rial (YER)
    ZAR => South African Rand (ZAR)
    ZMK => Zambian Kwacha (1968-2012) (ZMK)
    ZMW => Zambian Kwacha (ZMW)
    ZWL => Zimbabwean Dollar (2009) (ZWL)


Convert currency using google finance calculator in php Convert currency using google finance calculator in php Reviewed by JS Pixels on November 27, 2013 Rating: 5

19 comments:

  1. echo convertCurrency(1, "USD", "INR");

    how do i get the from and to currenciies and the amount from a webform, where the user selects the currencies and amount

    ReplyDelete
  2. Create a form with three fields, one field for currency and other two are select boxes with above given currency types, first for "from" currency second for "to" currency. Get value of all three fields and pass in the function echo convertCurrency(1, "AED", "INR");

    ReplyDelete
  3. Hi.,
    I used this code in my project but in some systems im not getting the result at $data = file_get_contents($url); can you please tell me why it is not coming.

    ReplyDelete
    Replies
    1. Hi,
      Instead of using file_get_contents($url) you can use curl options

      Delete
  4. Hi,
    i would like to know how to save all these conversion to mysql database, please help me

    ReplyDelete
  5. Altaf,

    Works wonderful. Thank you for this contribution.

    Regards.

    ReplyDelete
  6. can u please provide source code for this..
    thanks

    ReplyDelete
  7. @ Hello Pritam
    Source code is already given in this post. Please see above.

    ReplyDelete
  8. Here your code was returning 0.

    The fix was an easy one.

    I changed "https://" to "http://" and everything is working fine.

    Any idea why?

    ReplyDelete
    Replies
    1. Yes, That is an SSL issue.
      May be you are running this code on your local machine where file_get_contents() might be not configured for https wrapper.
      Try using curl() instead of file_get_contents()

      Delete
  9. thanks Altif nice blog
    Works wonderful

    ReplyDelete
  10. There is an error in ur code make it
    $converted = preg_replace("/[^0-9.]/", "", $converted[1]);
    to
    $converted = preg_replace("/[^0-9.]/", "", $converted[0]);

    ReplyDelete
    Replies
    1. @Ashutosh

      $converted[0] returns html and
      $converted[1] returns plain text

      You can use both if you are using above code, but $converted[1] is better choice.

      Delete
  11. It is working fine for me, but I need to know if it is possible I can convert an whole array through this API because I need to convert 15 values at once but if I am using through loop it is taking too long time.

    ReplyDelete
  12. Replies
    1. Google has changed its url
      from https://www.google.com/finance/converter
      to https://finance.google.com/finance/converter

      I have updated the url in script. It is working now. Thanks for pointing it out.

      Delete

Altaf Web. Powered by Blogger.