I have a file that will be called via CRON for 30 to 30 minutes, but would like some tips to improve it, because the way he is is VERY slow.
His logic is simple:
Search the database all records with status not ready
If records exist, it does a foreach, and for each instance of this class it calls another class to do a curl to grab an xml transforms it into md5 hash and compares the hash with the current database.
If the hashes are different it does other things
The problem is that this process is very slow, if someone can improve this?
<?php
require_once($_SERVER['DOCUMENT_ROOT'] . 'setup.php');
$MySQL = new MySQL();
$resultados = $MySQL->query("SELECT * FROM objetos WHERE situacao != 'entregue' AND email = 1")->fetchAll(PDO::FETCH_ASSOC);
if ($resultados) {
foreach ($resultados as $resultado) {
$Rastreio = new Correios(new cURL(), $resultado['cod_objeto']);
if ($Rastreio->resultado['hash'] != $resultado['hash']) {
$user = $MySQL->query("SELECT nome, email FROM usuarios WHERE id_usuario = {$resultado['fk_id_usuario']}")->fetch(PDO::FETCH_LAZY);
// Doing something different
$MySQL->exec("UPDATE objetos SET situacao = $Rastreio->resultado['status'], hash = {$Rastreio->$resultado['hash']} WHERE cod_objeto = {$resultado['cod_objeto']} AND fk_id_usuario = {$resultado['fk_id_usuario']}");
}
}
}
All answers were useful. I think the problem is in the database, my queries are not optimized as well. I'll try what you told ....
curlor you simply have or$resultadosis simply a very large array in which case there might not be much you can do about it.