Last active
August 16, 2024 02:05
-
-
Save giansalex/7f98a378265902672324469ccbc67b46 to your computer and use it in GitHub Desktop.
Query RUC list - SUNAT
This file contains hidden or 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 hidden or 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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yo estoy partiendo el txt en partes mas pequeñas para que la lectura sea mas rapida, aqui les paso el bash que uso:
#!/bin/bash
wget http://www2.sunat.gob.pe/padron_reducido_ruc.zip
unzip padron_reducido_ruc.zip
for i in {100..299}; do
echo "Cortando: ruc_$i \n\r"
grep "^$i" padron_reducido_ruc.txt > "txt/ruc_$i.txt"
done
chmod -R 777 txt/ruc_* -f
luego para leer
codigo PHP
set_time_limit(0);
function buscarBase($ruc){
$nro = substr($ruc, 0, 3);
$base = getcwd()."/base/txt/ruc_{$nro}.txt";
if (file_exists($base)) {
return $base;
}
return false;
}
function queryRucPadron($ruc)
{
$data=["resp"=>false,"data"=>""];
$base = buscarBase($ruc);
if (!$base)
return $data;
}
$XUR = "10203040500";
echo json_encode(queryRucPadron($XUR));