Skip to content

Instantly share code, notes, and snippets.

@erajuan
Created July 28, 2025 13:56
Show Gist options
  • Save erajuan/4bc4ff38e2b4b4954cc21196d70d8803 to your computer and use it in GitHub Desktop.
Save erajuan/4bc4ff38e2b4b4954cc21196d70d8803 to your computer and use it in GitHub Desktop.
Consulta de api ruc sunat, api dni reniec y tipo de cambio sunat con PHP
<?php
class DecolectaApiClient
{
private string $apiToken;
private string $baseUrl = "https://api.decolecta.com";
public function __construct(string $apiToken)
{
$this->apiToken = $apiToken;
}
private function get(string $endpoint, array $params = []): ?array
{
$url = $this->baseUrl . $endpoint;
if (!empty($params)) {
$url .= '?' . http_build_query($params);
}
$headers = [
"Authorization: Bearer {$this->apiToken}",
"Referer: php-decolecta"
];
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $headers,
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode === 200 && $response !== false) {
return json_decode($response, true);
}
error_log("Decolecta error [{$httpCode}] - {$url}");
return null;
}
public function getPersonByDni(string $dni): ?array
{
return $this->get("/v1/reniec/dni", ['numero' => $dni]);
}
public function getCompanyByRuc(string $ruc): ?array
{
return $this->get("/v1/sunat/ruc", ['numero' => $ruc]);
}
public function getExchangeRateByDate(string $date): ?array
{
return $this->get("/v1/tipo-cambio/sunat", ['date' => $date]);
}
public function getExchangeRateToday(): ?array
{
return $this->get("/v1/tipo-cambio/sunat");
}
public function getExchangeRateByMonth(int $month, int $year): ?array
{
return $this->get("/v1/tipo-cambio/sunat", ['month' => $month, 'year' => $year]);
}
}
@erajuan
Copy link
Author

erajuan commented Jul 28, 2025

ejemplo de de uso:

<?php

require_once 'DecolectaApiClient.php';

$client = new DecolectaApiClient('sk_123.tu_token_aqui');

// Obtener persona por DNI
$persona = $client->getPersonByDni('12345678');
print_r($persona);

// Obtener empresa por RUC
$empresa = $client->getCompanyByRuc('20123456789');
print_r($empresa);

// Tipo de cambio hoy
$tipoCambio = $client->getExchangeRateToday();
print_r($tipoCambio);

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