peru-consult
peru-consult copied to clipboard
Esta app no es estable, funciona siempre en cuando se intente 2 o tres veces
Esta app no es estable, funciona siempre en cuando se intente 2 o tres veces, no funciona en la primera, solo a veces funciona, creo que debe tener un retardo algunos milisegundos
Este repo se conecta a servidores externos, por que la estabilidad depende de esos servicios.
Para no depender de servicios de terceros, para el caso de: RUC- se puede usar el padron reducido, aunque no contiene tanta información como este servicio, puede ser util para la mayoria de casos. DNI- se puede tener una base de datos descentralizada validada por varias fuentes.
En algunos proyectos implementan un retardo de 5ms hasta que cargue el html y validan la clase list-group para recien parcear todo, pero en curl no se cómo se hará eso, creo que no se puede analizar el retorno del html.
Yo lo que hice fue lo siguiente:
Reemplaza la funcion post en el archivo: giansalex\peru-consult\src\Peru\Http\CurlClient.php.
public function post(string $url, $data, array $headers = [])
{
$raw = is_array($data) ? http_build_query($data) : $data;
$this->setDefaultConfig($url, $headers);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $raw);
return curl_exec($this->ch);
}
Por
public function post(string $url, $data, array $headers = [], $numRand = false, int $intentos = 0)
{
$raw = is_array($data) ? http_build_query($data) : $data;
$this->setDefaultConfig($url, $headers);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $raw);
$response = curl_exec($this->ch);
$info = curl_getinfo($this->ch);
if($numRand){
$lengthString = strlen($response);
if($info['http_code'] == 401 || $lengthString < 5000){
if($intentos < 6){
$intentos++;
return $this->post($url, $data, [], true, $intentos);
}
}
}
return $response;
}
Y en el archivo giansalex\peru-consult\src\Peru\Sunat\Ruc.php
Modifica
public function get(string $ruc): ?Company
{
$this->client->get(Endpoints::CONSULT);
$htmlRandom = $this->client->post(Endpoints::CONSULT, [
'accion' => 'consPorRazonSoc',
'razSoc' => 'BVA FOODS',
]);
$random = $this->getRandom($htmlRandom);
$html = $this->client->post(Endpoints::CONSULT, [
'accion' => 'consPorRuc',
'nroRuc' => $ruc,
'numRnd' => $random,
'actReturn' => '1',
'modo' => '1',
]);
return $html === false ? null : $this->parser->parse($html);
}
Por
public function get(string $ruc): ?Company
{
$this->client->get(Endpoints::CONSULT);
$htmlRandom = $this->client->post(Endpoints::CONSULT, [
'accion' => 'consPorRazonSoc',
'razSoc' => 'BVA FOODS',
], [], true);
$random = $this->getRandom($htmlRandom);
$html = $this->client->post(Endpoints::CONSULT, [
'accion' => 'consPorRuc',
'nroRuc' => $ruc,
'numRnd' => $random,
'actReturn' => '1',
'modo' => '1',
]);
return $html === false ? null : $this->parser->parse($html);
}
Lo que hace es que si no trae registro la primera vez que vuelva a enviar la petición hasta un máximo de 5 intentos.