Get Instant Access to our Exchange Rates API

Get access instantly - 1000 requests free p/m.


How To Use Our PHP Exchange Rates API

We offer an easy to use, accurate and reliable exchange rate API for PHP that's perfect for both personal and professional use.

Fetching exchange rates using our PHP currency conversion API is as easy as making GET requests. This means you can easily integrate our API into your PHP project however you'd prefer.

For more details on the different types of request we support please see our Main Documentation.

Alternatively simply use the PHP library code below to get started as fast as you can copy & paste!

Simple PHP Currency Conversion Example


// Fetching JSON
$req_url = 'https://v3.exchangerate-api.com/bulk/YOUR-API-KEY/USD';
$response_json = file_get_contents($req_url);

// Continuing if we got a result
if(false !== $response_json) {

    // Try/catch for json_decode operation
    try {

        // Decoding
        $response_object = json_decode($response_json);

        // Checking for errors
        if('success' === $response_object->result) {

            // YOUR APPLICATION CODE HERE, e.g.
            $base_price = 12; // Your price in USD
            $EUR_price = round(($base_price * $response_object->rates->EUR), 2);

        } else {

            // Handling different error conditions
            switch($response_object->error) {
                case 'unknown-code':
                    // Handle error...
                    break;
                case 'invalid-key':
                    // Handle error...
                    break;
                case 'malformed-request':
                    // Handle error...
                    break;
                case 'quota-reached':
                    // Handle error...
                    break;
            }

        }

    }
    catch(Exception $e) {
        // Handle JSON parse error...
    }

}
				

This will return the exchange rates from USD to all other currencies we support:

{
	"result": "success",
	"from": "USD",
	"rates": {
		"AUD": ((AUD in terms of USD)),
		"BGN": 1.8096,
		"BRL": 3.1143,
		"...": 1.3113,
		"...": 7.473, etc. etc.
	}
}

Please see our main documentation for further information on this query type & error response details.



We also offer automatic localization requests:



Automatic Currency Localization for PHP


// Getting visitor IP address
if(isset($_SERVER['HTTP_CLIENT_IP'])) {
    $visitor_ip = $_SERVER['HTTP_CLIENT_IP']; // For shared connections
} else if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $visitor_ip = $_SERVER['HTTP_X_FORWARDED_FOR']; // For proxy'd connections
} else {
    $visitor_ip = $_SERVER['REMOTE_ADDR']; // Everyone else
}

// Setting vars
$code = 'USD'; // Your base currency, 3 Letter Currency Code
$key  = 'API-KEY'; // Your API key
$req_url = 'https://v3.exchangerate-api.com/local/'.$key .'/'.$code.'/'.$visitor_ip;

// Fetching JSON
$response_json = file_get_contents($req_url);

// Continuing if we got a result
if(false !== $response_json) {

    // Try/catch for json_decode operation
    try {

        // Decoding
        $response_object = json_decode($response_json);

        // Proceeding only with valid JSON
        if(false !== $response_object)
            {

            // Checking for errors
            if('success' === $response_object->result) {

                // YOUR APPLICATION CODE HERE, e.g.
                $base_price = 12; // Your price in USD
                $converted_price = round(($base_price * $response_object->rate), 2);
                $converted_to_currency = $response_object->to;

                // Formatting currency symbol
                if(false === strpos($response_object->currency_symbol, ',')) {
                    $display_code = '&#x'.$response_object->currency_symbol.';';;
                } else {
                    $display_code = '';
                    $display_letters = explode(',', $response_object->currency_symbol);
                    $num_letters = count($display_letters);
                    for($i = 0;$i < $num_letters;$i++) {
                        $display_code .= '&#x'.$display_letters[$i].';';
                    }
                }

                // Outputting
                echo($display_code.$converted_price);

            } else {

                // Handling different error conditions
                switch($response_object->error) {
                    case 'unknown-code':
                        // Handle error...
                        break;
                    case 'invalid-key':
                        // Handle error...
                        break;
                    case 'malformed-request':
                        // Handle error...
                        break;
                    case 'quota-reached':
                        // Handle error...
                        break;
                }

            }

        }

    }
    catch(Exception $e) {
        // Handle JSON parse error...
    }

}
else {
    // Handle network error...
}
				

This will return the exchange rate from your base currency to the visitor currency & i18n data on your visitor.

In the example API response below your prices are in USD and a customer was visiting from a Bulgarian IP address so BGN is returned as the appropriate currency for a Bulgarian visitor.

{
	"result": "success",
	"from": "USD",
	"to": "BGN",
	"country_code": "BG",
	"currency_symbol": "43b,432",
	"rate": 1.81
}

Please see our main documentation for further information on this query type & error response details.

Additionally, please email us if you'd like to submit a better code example, we'll credit you!