Skip to content

How To Get Rice Rates API Using PHP

If you want to know how to get rice prices API using PHP, then you have found the perfect article for you.

Rice is the third-most-produced agricultural product in the world, after only sugarcane and maize, which clearly tells you the importance it has. China produced over 200 million tonnes of rice in 2019, followed by India, which produced 177 million tons. These high figures are due to the fact that rice is consumed by more than half of the world’s population and is mostly found in Asia, Latin America, and Africa.

If you invest in this sort of item, you should realize how crucial it is to be up to current on daily pricing. This might be the event that alters the trajectory of your investments. If you don’t monitor the commodity and observe how it performs in the market, you may end up with poor outcomes and lose money.

How To Get Rice Rates API Using PHP

Knowing this, you must be aware of its price in many currencies if you intend to develop your business or consider investing in the area. This may be performed through the use of an API, which is an interface that receives data from a location and then returns it to the person who initiated the API request. This is utilized on many websites and is quite useful when you need up-to-date information quickly.

These responses are mostly provided in JSON and Python. However, in this post, we will discuss PHP and where to get an API that supports that language.

Why PHP?

PHP is a popular open-source general-purpose programming language that can be incorporated into HTML and is great for web development. It may be used by both novices and specialists to construct static web pages, dynamic websites, and online apps.

PHP differs from client-side JavaScript in that the code is executed on the server, resulting in HTML that is subsequently sent to the client. The client would obtain the script’s execution results but have no clue what the underlying code was. You may even configure your webserver to use PHP to execute all of your HTML files, making it nearly hard for anyone to figure out what you’re up to.

How To Get The API

When looking for PHP rates, you must be cautious before selecting one or you may wind up wasting money and effort because not all of them function the same way.

For this example and to demonstrate how this works, we will utilize Commodities-API, a free web software that is currently the most comprehensive for obtaining the information we want.

How To Get Rice Rates API Using PHP

To use it, you need to:

1. Go to www.commodities-api.com and sign in to get your API key

2. Look for the symbols of Corn (CORN) and the currency of choice

3. Once you have them, insert them both and make the API call

4. The software will give you different answers, look for the PHP one between them. 

The answer in PHP will look something like this:

// set API Endpoint and API key 
$endpoint = 'latest';
$access_key = 'API_KEY';

// Initialize CURL:
$ch = curl_init('https://commodities-api.com/api/'.$endpoint.'?access_key='.$access_key.'');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Store the data:
$json = curl_exec($ch);
curl_close($ch);

// Decode JSON response:
$exchangeRates = json_decode($json, true);

// Access the exchange rate values, e.g. GBP:
echo $exchangeRates['rates']['GBP'];

How trustworthy is this information?

100%! Commodities-API is a software that collects its data from banks and the stock market, making it totally reliable. Once you make the API call, it gives it to you with a precision of 2 decimal points and in different programming languages for you to use. Furthermore, this program leverages bank-grade 256-bit SSL encryption to safeguard your connection.

Currency and commodity price conversions use the same API endpoints and may be used to convert any amount from one currency to another, from one commodity to any commodity, or from any currency to any currency.


Also published on Medium.

Published inAppsTechnology
%d bloggers like this: