PHP

Uso de PHP (Personal Home Page) para obtener datos desde la API de indicadores.

A continuación se muestra el código de ejemplo para consultar a nuestra API desde el Lenguaje PHP. El ejemplo utiliza cURL y envía la cabecera User-Agent para identificar el cliente.

<?php

$apiUrl = 'https://findic.cl/api/';

$curl = curl_init($apiUrl);
curl_setopt_array($curl, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CONNECTTIMEOUT => 5,
    CURLOPT_TIMEOUT => 10,
    CURLOPT_USERAGENT => 'findic-docs-php/1.0',
    CURLOPT_HTTPHEADER => [
        'Accept: application/json',
    ],
]);

$json = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$error = curl_error($curl);

if ($json === false || $status !== 200) {
    exit('Error en la solicitud: ' . ($error !== '' ? $error : $status));
}

try {
    // Decodificamos el JSON y lo transformamos en objeto
    $dailyIndicators = json_decode($json, false, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $exception) {
    exit('Error al decodificar el JSON: ' . $exception->getMessage());
}

// Imprimimos el dato de la UF
echo 'El valor actual de la UF es $' . $dailyIndicators->uf->valor;

Si tu servidor no tiene cURL habilitado, puedes usar file_get_contents con un contexto de flujo que incluya las mismas cabeceras User-Agent y Accept.

Más información del objeto JSON entregado en el resumen aquí.