PHP の例 – ヘルス フィットネス API¶
PHP は、複数の方法を使用して ヘルス フィットネス API とやり取りできます。 同期および高度な方法の例を以下に示します。
1. cURL (最も一般的な方法) を使用¶
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);
?>
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. file_get_contents (標準の PHP) を使用¶
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);
?>
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. GuzzleHttp (Composer パッケージ) を使用¶
Composer を通じてインストール:
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);
?>
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. Symfony HttpClient (Symfony アプリケーション向けに推奨) を使用¶
Composer を通じてインストール:
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);
?>
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. 注意事項とベストプラクティス¶
YOUR_API_KEYを実際のキーで置き換えてください。langパラメータを使用して、複数の言語に対応したレスポンスを取得します (en,fr,de,es, など)。- 安定した動作のために、HTTP エラーと例外を処理してください。
- 接続プーリング、再試行、および非同期サポートなどの利点から、高度なアプリケーションには Guzzle または Symfony HttpClient の使用を検討してください。
- API レスポンスには、利用可能な場合、リスク信号、アクションプラン、および
_enterpriseメタデータが含まれている場合があります。
次のステップ¶
その他の言語固有の例を確認してください:
- Python
- JavaScript
- NodeJs
- Kotlin / Android
- Flutter / Dart
- React Native / Expo
- Bash Shell
- Go / Golang
- Ruby / Rails
今すぐ PHP アプリケーションにヘルス フィットネス API を統合しましょう!
ヘルス フィットネス API 現代のデジタルヘルスインフラをサポート