-1
Good afternoon guys, I’m having a question/need that I haven’t found how to do. I have a code that I use to run several Cnpjs through the Sintegra API, where with the result returned by this I feed the information in a database. The problem is as follows, when it is returned a status other than OK of this API in the search, the System stops and does not continue to run the amount I determined in the Query. Is there any way to make a sort of queue, where when the code is different from OK, it just jumps to the next one and I didn’t try to run what is wrong ?
php query.:
<?php
session_start();
set_time_limit(0);
include "conexao.php";
error_reporting(E_ALL);
header('Content-Type: text/html; charset=utf-8');
$query = "SELECT id_fornecedor, cnpj FROM fornecedores WHERE status_consulta = 'N'";
$fornecedores = mysqli_fetch_all(mysqli_query($connect, $query), MYSQLI_ASSOC);
// Informações para a chamada da API
$token = "";
$plugin = "ST";
foreach ($fornecedores as $key => $fornecedor) {
//URL do serviço
$service_url = "https://sintegraws.com.br/api/v1/execute-api.php?token=" . $token . "&cnpj=" . $fornecedor['cnpj'] . "&plugin=" . $plugin;
//Faz a chamada da API
$response = file_get_contents($service_url);
//Aqui fazemos o parse do json retornado
$json = json_decode($response);
$nome_fornecedor = utf8_decode($json->nome_empresarial);
$situacao_cnpj = utf8_decode($json->situacao_cnpj);
$situacao_ie = utf8_decode($json->situacao_ie);
$mensagem = utf8_decode($json->message);
//Aqui exibimos uma mensagem caso tenha ocorrido algum erro
if ($json->status != 'OK') {
if ($json->message === "CNPJ inválido.") {
$query = "UPDATE fornecedores SET nome_fornecedor = NULL, situacao_cnpj = NULL, situacao_ie = NULL, status_consulta = 'P', mensagem = '$mensagem',
dtAtt = NOW() WHERE id_fornecedor = " . $fornecedor['id_fornecedor'];
} else {
die("Erro " . $json->code . ": " . $json->message);
}
}
$query = "UPDATE fornecedores SET nome_fornecedor = '$nome_fornecedor', situacao_cnpj = '$situacao_cnpj', situacao_ie = '$situacao_ie', status_consulta = 'P', mensagem = '$mensagem',
dtAtt = NOW() WHERE id_fornecedor = " . $fornecedor['id_fornecedor'];
if (mysqli_query($connect, $query)) {
$_SESSION['fornecedores_atualizados'] = true;
} else {
$_SESSION['fornecedores_erro'] = true;
}
}
index php.:
<?php
session_start();
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Atualiza Fornecedores - SINTEGRA</title>
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
<script src="script.js"></script>
<style>
#enviar {
padding: 13px 15px;
border: 1px solid green;
border-radius: 5px;
background: #138c13;
color: white;
font-weight: bold;
cursor: pointer;
}
</style>
</head>
<body>
<form action="consulta.php" method="POST">
<button type="submit" id="enviar">Atualizar Fornecedores</button> <br>
<p>Atualizados: </p>
<p>Desatualizados: </p>
</form>
<?php
if (isset($_SESSION['fornecedores_atualizados'])):
?>
<div>
<p>Fornecedores atualizados com sucesso !</p>
</div>
<?php
endif;
unset($_SESSION['fornecedores_atualizados']);
?>
<?php
if (isset($_SESSION['fornecedores_erro'])):
?>
<div>
<p> Houve um erro ao atualizar os fornecedores. <?= mysqli_error($connect) ?></p>
</div>
<?php
endif;
unset($_SESSION['fornecedores_erro']);
?>
</body>
</html>
The system runs as soon as I click on this button that is in Index, and I always put a range of Cnpjs in Query, is not in the code because I had already run all and just wanted to check the ones that had returned errors.
makes an if if you fall into the error it increments it to jump to the next... Maybe it solves, but is not the recommended.
– Diêgo Correia de Andrade
Can make it simpler, inside the block
if ($json->status != 'OK') {
, before the}else
add acontinue;
, it will simply go through the continue and will continue the process– Jakson Fischer