but these 4 years are included in the next currency loop.
I have no idea what this means
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have an API that provides me with historical cryptocurrency quote data. However, it provides me with data every 4 years, so I have to repeat the request until I finish the entire history of a currency. Currently I'm doing it like this:
public function intraday(){
$client = new Client();
$indices = IndiceCriptomoedas::all();
$dataInicial = '2013-11-01';
$dataFinal = '2017-11-01';
foreach($indices as $symbol){
$response = $client->request('GET', 'https://financialmodelingprep.com/api/v3/historical-chart/1day/'.$symbol->indice.'?from='.$dataInicial.'&to='.$dataFinal.'&apikey=KAKAKKAKKA');
$moedas = json_decode($response->getBody());
foreach ($moedas as $moeda) {
$novo = new CriptomoedasIntraday();
$novo->symbol = $symbol->indice;
$novo->close = $moeda->close;
$novo->close_price = number_format($moeda->close, 2, ',', '.');
$novo->volume = $moeda->volume;
$novo->date = $moeda->date;
$novo->save();
$dataInicial = date('Y-m-d', strtotime($dataInicial . ' +4 years'));
$dataFinal = date('Y-m-d', strtotime($dataFinal . ' +4 years'));
$symbol->intraday_enviado = true;
$symbol->update();
}
}
}
But it happens that after saving the first batch to the database, I call the code to include another 4 years in the start and end date, but these 4 years are included in the next currency loop. How do I do it and what would be the best way for me to go through the entire history of a currency, and then go to the next one?
@mvnobrega either way, you need two nested loops, one for the years and one for the symbol.
You only have one loop at present, and also no way of knowing when to stop.
public function intraday()
{
$indices = IndiceCriptomoedas::all();
$client = new Client();
foreach(range(2013, today()->year) as $year) {
foreach($indices as $symbol) {
$response = $client->request('GET', 'https://financialmodelingprep.com/api/v3/historical-chart/1day/'.$symbol->indice.'?from=' . $year . '11-01&to=' . $year+1 . '11-01&apikey=KAKAKKAKKA');
$moedas = json_decode($response->getBody());
foreach ($moedas as $moeda) {
$novo = new CriptomoedasIntraday();
$novo->symbol = $symbol->indice;
$novo->close = $moeda->close;
$novo->close_price = number_format($moeda->close, 2, ',', '.');
$novo->volume = $moeda->volume;
$novo->date = $moeda->date;
$novo->save();
$symbol->intraday_enviado = true;
$symbol->update();
}
}
}
Please or to participate in this conversation.