Saltar a contenido

Ejemplos de PHP – API de Salud y Fitness

PHP puede interactuar con la API de Salud y Fitness utilizando múltiples enfoques. Aquí hay ejemplos tanto para métodos sincrónicos como avanzados.


1. Usando cURL (el más común)

Ejemplo de solicitud GET:

<?php
$apiKey = "YOUR_API_KEY";
$url = "https://api.hefitapi.com/api/v1/bmi?height=178&weight=82&units=metric&lang=en";

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "X-API-Key: $apiKey"
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response, true);
print_r($data);
?>

Ejemplo de solicitud POST:

<?php
$apiKey = "YOUR_API_KEY";
$url = "https://api.hefitapi.com/api/v1/bmi/post";

$payload = json_encode([
    "height" => 178,
    "weight" => 82,
    "units" => "metric"
]);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "X-API-Key: $apiKey",
    "Content-Type: application/json"
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);

$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response, true);
print_r($data);
?>

2. Usando file_get_contents (PHP estándar)

Ejemplo de solicitud GET:

<?php
$apiKey = "YOUR_API_KEY";
$url = "https://api.hefitapi.com/api/v1/bmi?height=178&weight=82&units=metric&lang=en";

$options = [
    "http" => [
        "header" => "X-API-Key: $apiKey\r\n",
        "method" => "GET"
    ]
];

$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);

$data = json_decode($response, true);
print_r($data);
?>

Ejemplo de solicitud POST:

<?php
$apiKey = "YOUR_API_KEY";
$url = "https://api.hefitapi.com/api/v1/bmi/post";

$payload = json_encode([
    "height" => 178,
    "weight" => 82,
    "units" => "metric"
]);

$options = [
    "http" => [
        "header" => "Content-Type: application/json\r\nX-API-Key: $apiKey\r\n",
        "method" => "POST",
        "content" => $payload
    ]
];

$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);

$data = json_decode($response, true);
print_r($data);
?>

3. Usando GuzzleHttp (Paquete de Composer)

Instalar mediante Composer:

composer require guzzlehttp/guzzle

Ejemplo de solicitud GET:

<?php
require 'vendor/autoload.php';

use GuzzleHttp\Client;

$client = new Client();
$apiKey = "YOUR_API_KEY";

$response = $client->request('GET', 'https://api.hefitapi.com/api/v1/bmi', [
    'headers' => ['X-API-Key' => $apiKey],
    'query' => ['height' => 178, 'weight' => 82, 'units' => 'metric', 'lang' => 'en']
]);

$data = json_decode($response->getBody(), true);
print_r($data);
?>

Ejemplo de solicitud POST:

<?php
$response = $client->request('POST', 'https://api.hefitapi.com/api/v1/bmi/post', [
    'headers' => ['X-API-Key' => $apiKey, 'Content-Type' => 'application/json'],
    'json' => ['height' => 178, 'weight' => 82, 'units' => 'metric']
]);

$data = json_decode($response->getBody(), true);
print_r($data);
?>

4. Usando Symfony HttpClient (Recomendado para aplicaciones Symfony)

Instalar mediante Composer:

composer require symfony/http-client

Ejemplo de solicitud GET:

<?php
require 'vendor/autoload.php';

use Symfony\Component\HttpClient\HttpClient;

$client = HttpClient::create();
$apiKey = "YOUR_API_KEY";

$response = $client->request('GET', 'https://api.hefitapi.com/api/v1/bmi', [
    'headers' => ['X-API-Key' => $apiKey],
    'query' => ['height' => 178, 'weight' => 82, 'units' => 'metric', 'lang' => 'en']
]);

$data = $response->toArray();
print_r($data);
?>

Ejemplo de solicitud POST:

<?php
$response = $client->request('POST', 'https://api.hefitapi.com/api/v1/bmi/post', [
    'headers' => ['X-API-Key' => $apiKey, 'Content-Type' => 'application/json'],
    'json' => ['height' => 178, 'weight' => 82, 'units' => 'metric']
]);

$data = $response->toArray();
print_r($data);
?>

5. Notas y Mejores Prácticas

  • Reemplace YOUR_API_KEY con su clave real.
  • Use el parámetro lang para obtener respuestas en varios idiomas (en, fr, de, es, etc.).
  • Maneje los errores y excepciones HTTP para una mayor fiabilidad en producción.
  • Considere usar Guzzle o Symfony HttpClient para aplicaciones avanzadas debido al almacenamiento en caché, los reintentos y el soporte asíncrono.
  • Las respuestas de la API pueden incluir señales de riesgo, planes de acción y _enterprise metadatos cuando estén disponibles.

Próximos pasos

Consulte otros ejemplos específicos del idioma:

¡Comience a integrar la API de Salud y Fitness en sus aplicaciones PHP hoy mismo!


API de Salud y Fitness Impulsando la infraestructura digital de salud moderna