Last active
April 30, 2022 23:12
-
-
Save giansalex/7f98a378265902672324469ccbc67b46 to your computer and use it in GitHub Desktop.
Query RUC list - SUNAT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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|-|-|-|-| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$line = 9623416; | |
$spl = new SplFileObject('padron_reducido_ruc.txt'); | |
$spl->seek($line); | |
echo $spl->current().PHP_EOL; |
Mi Bro, el codigo esta OK, pero demora demasiado
Claro que va a demorar, es un TXT con muchos registros.
Buen aporte, pero incluso en una base de sql demora, lo tengo automatizado, se descarga y paso los datos a sql server. Salu2.
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
Mi Bro, el codigo esta OK, pero demora demasiado