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 (कंपोजर पैकेज) का उपयोग करना¶
कंपोजर के माध्यम से स्थापित करें:
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 (सिंफोनी ऐप्स के लिए अनुशंसित) का उपयोग करना¶
कंपोजर के माध्यम से स्थापित करें:
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मेटाडेटा शामिल हो सकते हैं, जब उपलब्ध हों।
अगले चरण¶
अन्य भाषा-विशिष्ट उदाहरण देखें:
- पायथन
- जावास्क्रिप्ट
- नोडजेएस
- कोटलिन / एंड्रॉइड
- फ्लटर / डार्ट
- रिएक्ट नेटिव / एक्सपो
- बैश शेल
- गो / गोलांग
- रूबీ / रैल्स
आज ही अपने PHP ऐप्स में हेल्थ फिटनेस एपीआई को एकीकृत करना शुरू करें!
हेल्थ फिटनेस एपीआई आधुनिक डिजिटल स्वास्थ्य बुनियादी ढांचे को सशक्त बनाना