Created
July 28, 2025 13:56
-
-
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
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 | |
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]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ejemplo de de uso: