Forex REST JSON API with PHP

Forex REST JSON API with PHP

In this tutorial, we will focus on setting up a PHP environment and getting Live Forex, CFD, and Crypto data via REST JSON API from the command line. You can run PHP on the server side, and we'll cover that in another tutorial.

Open a command window and type "php.exe -version" to see if PHP is already installed. If it returns a version number, you can move on to the coding part; otherwise, you must install PHP. You can sign up for free and then grab the key or a pre-populated code sample from our data docs page. You will need an API key for the FX data for this tutorial.

Configure PHP

PHP configuration is really straightforward to implement and only requires three simple steps.

  • From the PHP website, download the zip file that contains php.exe, and then unzip it into the directory of your choice.

  • Include the path variable under "Environment Variables" in Windows with the location of your php.exe.

  • To check your installation, open a command window and enter the commands below. You should see the version number shown.

php.exe -version

You will get the PHP version number as an output similar to 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 can activate the curl function by uncommenting the following 2 lines in your php.ini file.

extension_dir = ".ext"

extension=curl

Let's start coding in PHP now.

Make a directory to keep your code, then name it "tms json.php"; mine has that extension. The file identifier?php and the init statement for the Curl software, which is used to call the server and retrieve the response, are added to this file.

?php

$curl = curl_init();

Next, we build the curl setopt array, which will be used to configure the curl request we will issue and is called by the curl exec command. For this example, we'll set the CURLOPT URL to the live endpoint; you'll need to enter your key where necessary.

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

));

To get the error buffer and retrieve the data for the Forex and CFD markets, we will now call execute the Curl.

$response = curl_exec($curl)

$error = curl_error($curl)

After making the call, we will now examine the outcome. We ensure the error buffer is empty and output the error if it is not. We can output the prices we received if the error buffer is empty.

if ($err) {

echo "cURL Error #:" . $err;

} else {

echo $response;

}

Here is the complete code; all you have to do is enter 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 after that, run the software to see the current 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

}

If you have any queries about the tutorial, please don't hesitate to get in touch with us. Additionally, we stand ready to support aspiring programmers, fledgling businesses, and well-established organisations as they work to offer cutting-edge solutions to their clientele. If you need a custom solution, get in touch with us; otherwise, sign up for our Free Forex API.

Also, refer to the originally published tutorial on our website: Forex REST JSON API with PHP

Also, go through the following PHP-based technical tutorials to upskill in coding and data retrieval:

  1. PHP WebSocket Client

  2. Convert Real-Time Forex Data into OHLC Bars with PHP