I
I
Ilya Parshakov2018-03-19 17:38:11
API
Ilya Parshakov, 2018-03-19 17:38:11

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;
    }

Please tell me how to do it right?
I couldn’t google anything on this topic, share a link if it’s not difficult, with advice, maybe you need to use some bibles for this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JhaoDa, 2018-03-19
@parschakov

1. Use guzzle/guzzle instead of curl.
2. Cache this data for several hours.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question