Answer the question
In order to leave comments, you need to log in
How to work with a third-party API?
Hello!
For educational purposes, I decided to try to work with a third-party API and a few questions arose.
For example, there is an API that provides the exchange rate. I made CRUD and a page that shows for example the currency I bought, at what rate and quantity. With the API, up-to-date information on exchange rates is pulled up and I see, for example, how much in rubles I am in the red or in the black, depending on the fall / increase in the exchange rate.
But I ran into a problem, how to work with API data correctly? Does it make sense to store them in the database and then work from the database? Or load data from the API with each request?
For example, I need data on several currencies and then make calculations based on their rates [amount of currency * (current_rate - purchase_rate)]. And such currencies, for example, pieces 5-10.
public function getCourses()
{
$url = 'https://www.cbr-xml-daily.ru/daily_json.js';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = json_decode(curl_exec($ch));
return $result;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question