Aller au contenu

Exemples PHP – API de Santé et de Fitness

PHP peut interagir avec l'API de Santé et de Fitness en utilisant différentes approches. Voici des exemples pour les méthodes synchrones et avancées.


1. Utilisation de cURL (la plus courante)

Exemple de requête 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);
?>

Exemple de requête 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. Utilisation de file_get_contents (PHP standard)

Exemple de requête 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);
?>

Exemple de requête 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. Utilisation de GuzzleHttp (package Composer)

Installation via Composer :

composer require guzzlehttp/guzzle

Exemple de requête 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);
?>

Exemple de requête 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. Utilisation de Symfony HttpClient (recommandé pour les applications Symfony)

Installation via Composer :

composer require symfony/http-client

Exemple de requête 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);
?>

Exemple de requête 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. Notes et meilleures pratiques

  • Remplacez YOUR_API_KEY par votre clé.
  • Utilisez le paramètre lang pour obtenir des réponses multilingues (en, fr, de, es, etc.).
  • Gérez les erreurs et exceptions HTTP pour une fiabilité en production.
  • Envisagez d'utiliser Guzzle ou Symfony HttpClient pour les applications avancées en raison du pool de connexions, des tentatives et du support asynchrone.
  • Les réponses API peuvent inclure des signaux de risque, des plans d'action et des métadonnées _enterprise si disponibles.

Prochaines étapes

Consultez d'autres exemples spécifiques aux langages :

Commencez à intégrer l'API de Santé et de Fitness dans vos applications PHP dès aujourd'hui !


API de Santé et de Fitness Alimentant l'infrastructure numérique de santé moderne