PHP Beispiele – Health Fitness API¶
PHP kann mit der Health Fitness API mithilfe verschiedener Ansätze interagieren. Hier sind Beispiele für sowohl synchrone als auch fortgeschrittene Methoden.
1. Verwendung von cURL (am häufigsten)¶
Beispiel für eine GET-Anfrage:
<?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);
?>
Beispiel für eine POST-Anfrage:
<?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. Verwendung von file_get_contents (Standard PHP)¶
Beispiel für eine GET-Anfrage:
<?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);
?>
Beispiel für eine POST-Anfrage:
<?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. Verwendung von GuzzleHttp (Composer-Paket)¶
Installation über Composer:
Beispiel für eine GET-Anfrage:
<?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);
?>
Beispiel für eine POST-Anfrage:
<?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. Verwendung von Symfony HttpClient (Empfohlen für Symfony-Anwendungen)¶
Installation über Composer:
Beispiel für eine GET-Anfrage:
<?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);
?>
Beispiel für eine POST-Anfrage:
<?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. Hinweise und Best Practices¶
- Ersetzen Sie
YOUR_API_KEYdurch Ihren tatsächlichen Schlüssel. - Verwenden Sie den Parameter
lang, um mehrsprachige Antworten zu erhalten (en,fr,de,es, usw.). - Behandeln Sie HTTP-Fehler und Ausnahmen für eine zuverlässige Produktion.
- Erwägen Sie die Verwendung von Guzzle oder Symfony HttpClient für fortgeschrittene Anwendungen, da diese Verbindungspooling, Wiederholungsversuche und asynchrone Unterstützung bieten.
- API-Antworten können Risikosinformationen, Aktionspläne und
_enterpriseMetadaten enthalten, falls verfügbar.
Nächste Schritte¶
Überprüfen Sie andere sprachspezifische Beispiele:
- Python
- JavaScript
- NodeJs
- Kotlin / Android
- Flutter / Dart
- React Native / Expo
- Bash Shell
- Go / Golang
- Ruby / Rails
Beginnen Sie noch heute mit der Integration der Health Fitness API in Ihre PHP-Anwendungen!
Health Fitness API Ermöglicht moderne digitale Gesundheitsinfrastruktur