Skip to content

Instantly share code, notes, and snippets.

@giansalex
Last active April 30, 2022 23:12
Show Gist options
  • Save giansalex/7f98a378265902672324469ccbc67b46 to your computer and use it in GitHub Desktop.
Save giansalex/7f98a378265902672324469ccbc67b46 to your computer and use it in GitHub Desktop.
Query RUC list - SUNAT
<?php
// Descargar padron reducido: https://www.sunat.gob.pe/descargaPRR/mrc137_padron_reducido.html
// Resultado al descomprimir: padron_reducido_ruc.txt
set_time_limit(0);
function queryRucPadron($txtPath, $ruc)
{
$handle = fopen($txtPath, "r") or die("No se puede abrir el txt");
$lines = 0;
$isFirst = true;
while (!feof($handle)) {
$line = fgets($handle, 1024);
if ($isFirst) {
$isFirst = false;
$lines++;
continue;
}
if (substr( $line, 0, 11) === $ruc) {
// position: $lines
return utf8_encode($line);
}
$lines++;
}
fclose($handle);
return 'NO ENCONTRADO';
}
// Este proceso toma unos cuantos segundos.
$ruc = '20100070970';
echo 'search: '.$ruc.PHP_EOL;
$resultado = queryRucPadron('padron_reducido_ruc.txt', $ruc);
echo $resultado.PHP_EOL;
// php query-ruc-padron.php
// search: 20100070970
// 20100070970|SUPERMERCADOS PERUANOS SOCIEDAD ANONIMA 'O ' S.P.S.A.|ACTIVO|HABIDO|150130|CAL.|MORELLI|-|-|181|P-2|-|-|-|-|
<?php
$line = 9623416;
$spl = new SplFileObject('padron_reducido_ruc.txt');
$spl->seek($line);
echo $spl->current().PHP_EOL;
@giansalex
Copy link
Author

En aquel momento, también exporte a una base de datos de un solo archivo https://www.php.net/manual/es/dba.example.php para evitar la dependencia de base de datos externa, los tiempos eran aceptables.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment