BackForex API PHP

Forex REST JSON API with PHP

12 November 2021


In this tutorial, you will learn how to set up a PHP environment and request Live Forex, CFD, and Crypto data using REST JSON API from the command line. You can also run PHP on the server side, which will be covered in a separate tutorial.


You can test if you have PHP 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 simple and can be done in 3 easy steps.

  • Download the zip file containing the php.exe from the PHP site and unzip it into a directory of your choice.
  • Add the location of your php.exe to your path variable under "Environment Variables" on Windows.
  • 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". In the file, we include the file identifier ?php and the init statement for the Curl program. Curl is used to contact the server and receive a response.

?php

$curl = curl_init();

Next, we use curl_setopt_array to set up the curl request before running curl_exec. In this example, we will set the CURLOPT_URL to the live endpoint and you must insert your key where needed.

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 contact us, if you have any questions regarding the tutorial. We are here to help new programmers, startups, and established companies create advanced solutions for their customers. Get in contact if you are looking for a bespoke solution alternatively, subscribe to our Free Forex API.