Forex REST JSON API with PHP

11/12/2021 at 04:12PM

Learn setting up a PHP environment and requesting Live Forex, CFD, and Crypto data via REST JSON API from the command line, you can also run PHP on the server-side and we will cover that in another tutorial.

You can test if you have PHP already installed by opening command windows and typing "php.exe -version" if you get a version number output you can skip to the coding section, if not you will need to install PHP. For this tutorial, you will need an API key for the forex data, you can sign up for Free and then download the key or a pre-populated code sample from our data docs page.

Setup PHP

Setting up PHP is very simple a can be done in 3 easy steps.

  1. Download the zip file containing the php.exe from the PHP site and unzip it into a directory of your choice.
  2. Add the location of your php.exe to your path variable under "Environment Variables" in windows.
  3. Verify your install, open a command window and run the following you should get a version number output.
php.exe -version

and you should get something like this

PHP 8.0.12 (cli) (built: Oct 19 2021 11:23:03) ( NTS Visual C++ 2019 x64 )
Copyright (c) The PHP Group
Zend Engine v4.0.12, Copyright (c) Zend Technologies

You will also need to enable curl in your php.ini by uncommenting the following 2 lines.

extension_dir = ".ext"
extension=curl

Now let's do some PHP coding

Create a directory to store your code and then create a file with the extension .php, mine is called "tms_json.php". Into this file we add the file identifier ?php and the init statement for the Curl program, Curl is what we use to call the server and get the response.

?php

$curl = curl_init();

Next, we create the curl_setopt_array this will set up the curl request we are going to make and this will be called when we run the curl_exec command. For this example, we are going to set the CURLOPT_URL to the live endpoint and you will need to insert your key where required.

curl_setopt_array( $curl, array(
  CURLOPT_PORT => "443",
  CURLOPT_URL => "https://marketdata.tradermade.com/api/v1/live?currency=EURUSD,GBPUSD,UK100&api_key=YOUR_API_KEY)",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_SSL_VERIFYPEER => 0 
));

Now we will call execute Curl and retrieve Forex and CFD data, we will also retrieve the error buffer to check for any problems with the call.

$response = curl_exec($curl)
$error = curl_error($curl)

Now we have executed the call we will check the response. We check that the error buffer is empty and if it's not we output the error. If the error buffer is empty we can then output the prices we received.

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

Here is the full code you just need to set your API key.

<?php

$curl = curl_init();

curl_setopt_array( $curl, array(
  CURLOPT_PORT => "443",
  CURLOPT_URL => "https://marketdata.tradermade.com/api/v1/convert?from=EUR&to=USD&amount=1000&api_key=YOUR_API_KEY",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_SSL_VERIFYPEER => 0
));

$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}
?>

and then run the program and you should get the live Forex and CFD rates.

php TraderMadeDataGetter.php

Output

{
  "base_currency": "EUR",
  "endpoint": "convert",
  "quote": 1.12733,
  "quote_currency": "USD",
  "requested_time": "Wed, 08 Dec 2021 11:46:15 GMT",
  "timestamp": 1638963975,
  "total": 1127.33
}

Please feel free to contact us if you have any questions regarding the tutorial. Also, we are always ready to help budding programmers, startups, and established institutions in their endeavour to provide high-tech solutions to their clients. Get in contact if you are looking for a bespoke solution alternatively subscribe to our Free Forex API.

Related Posts