أمثلة PHP – واجهة برمجة تطبيقات الصحة واللياقة البدنية¶
يمكن لـ PHP التفاعل مع واجهة برمجة تطبيقات الصحة واللياقة البدنية باستخدام عدة طرق. فيما يلي أمثلة للطرق المتزامنة والمتقدمة.
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 لتطبيقات متقدمة نظرًا لدعم التجميع، والعودات، والدعم غير المتزامن.
- قد تتضمن استجابات واجهة برمجة التطبيقات إشارات المخاطر، وخطط العمل، ومعلومات
_enterpriseعند توفرها.
الخطوات التالية¶
تحقق من الأمثلة الخاصة باللغات الأخرى:
- Python
- JavaScript
- NodeJs
- Kotlin / Android
- Flutter / Dart
- React Native / Expo
- Bash Shell
- Go / Golang
- Ruby / Rails
ابدأ في دمج واجهة برمجة تطبيقات الصحة واللياقة البدنية في تطبيقات PHP الخاصة بك اليوم!
واجهة برمجة تطبيقات الصحة واللياقة البدنية تمكين البنية التحتية الرقمية للرعاية الصحية الحديثة