EUR/USD 1.15332 +0.09% GBP/USD 1.34816 +0.16% USD/JPY 159.042 -0.71% USD/CHF 0.80797 +0.28% AUD/USD 0.70381 +0.19% USD/CAD 1.40173 +0.05% NZD/USD 0.58900 +0.27% BTC/USD 62,934.50 -2.74% ETH/USD 1,865.78 -2.65% · EUR/USD 1.15332 +0.09% GBP/USD 1.34816 +0.16% USD/JPY 159.042 -0.71% USD/CHF 0.80797 +0.28% AUD/USD 0.70381 +0.19% USD/CAD 1.40173 +0.05% NZD/USD 0.58900 +0.27% BTC/USD 62,934.50 -2.74% ETH/USD 1,865.78 -2.65% ·
3 min read forex api php

How to use a very simple PHP Forex API

Sooner or later, there will be a day in your developer career when you need to power your web or mobile PHP application with real-time financial data.

How to use a very simple PHP Forex API

Every PHP developer eventually hits the moment when a web or mobile app needs real-time financial data. Most of the obvious options fall short: too complex to implement, refresh rates that lag behind the market, or price tags that don't fit the project.

The good news is that pulling live FX rates from the Live-Rates REST API in PHP takes only a handful of lines of code.

Key takeaways

  • Live-Rates exposes forex data through a plain JSON REST endpoint at https://www.live-rates.com/rates.
  • PHP can consume it with file_get_contents and json_decode — no SDK required.
  • Prices update every second, keeping applications aligned with the live market.
  • Filtering a single pair such as EUR/USD is a one-line conditional inside the loop.
  • Removing that conditional returns quotes for every available instrument.

Why a Simple PHP Forex API Matters

Financial data providers often overwhelm developers with heavy SDKs, delayed feeds, or enterprise pricing. For a PHP application that just needs current bid, ask, and timestamp values, that overhead is unnecessary. A lightweight JSON endpoint keeps integration friction low and lets the app focus on its actual product.

Fetching Rates From the Live-Rates Endpoint

The endpoint https://www.live-rates.com/rates returns a JSON feed of currency pairs. PHP's built-in functions are enough to retrieve and parse it.

Minimal PHP Example

<?php
$url = 'https://www.live-rates.com/rates'; // path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$json = json_decode($data); // decode the JSON feed

foreach($json as $obj){
    if ($obj->currency == 'EUR/USD'){
        echo "Pair: $obj->currency\n";
        echo "Bid: $obj->bid\n";
        echo "Ask: $obj->ask\n";
        echo "Timestamp: $obj->timestamp\n";
    }
}

The snippet above prints updated prices for EUR/USD, including bid, ask, and timestamp for each tick.

Filtering Pairs or Returning the Full List

The if statement inside the loop acts as the filter. Keep it in place to isolate a single instrument, or comment it out to iterate through every pair the feed returns.

if ($obj->currency == 'EUR/USD'){

With that line removed, the same loop prints the full universe of instruments delivered by the endpoint.

Working With the Response Fields

Each entry in the JSON feed exposes the fields used in the example: currency, bid, ask, and timestamp. These map directly to properties on the decoded object, so accessing them with the -> operator is all that's required to surface live quotes inside a PHP application.

Beyond PHP: Other Languages

Developers working outside PHP can apply the same pattern in other stacks. For JavaScript and Node.js integrations, see the companion guide How to Retrieve JSON Forex Data in Javascript.

FAQ

What endpoint does Live-Rates provide for PHP integrations?

The JSON feed is served at https://www.live-rates.com/rates and can be consumed directly with file_get_contents and json_decode.

How often are the rates updated?

The feed refreshes every second, which is what makes it suitable for applications that need real-time financial data.

How can a specific currency pair like EUR/USD be isolated?

Inside the foreach loop, an if statement comparing $obj->currency to 'EUR/USD' restricts output to that pair only.

How can every instrument be returned instead of just one pair?

Commenting out the if ($obj->currency == 'EUR/USD'){ line removes the filter, so the loop prints data for all instruments in the feed.

Which fields are available for each quote?

Each object exposes currency, bid, ask, and timestamp, accessible as properties on the decoded JSON.

Ready to plug real-time forex data into a PHP project? Explore the options on the Live-Rates plans page and start streaming quotes in minutes.

Real-time forex rates for your app

Live bid/ask for the pairs you need, updated every second, with a simple JSON & XML API. Try it free for 7 days.