Exemplos em PHP – API de Saúde e Fitness¶
O PHP pode interagir com a API de Saúde e Fitness utilizando múltiplas abordagens. Aqui estão exemplos para métodos sincrónos e avançados.
1. Utilizando cURL (mais comum)¶
Exemplo de requisição 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);
?>
Exemplo de requisição 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. Utilizando file_get_contents (PHP padrão)¶
Exemplo de requisição 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);
?>
Exemplo de requisição 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. Utilizando GuzzleHttp (Pacote do Composer)¶
Instale via Composer:
Exemplo de requisição 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);
?>
Exemplo de requisição 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. Utilizando Symfony HttpClient (Recomendado para aplicações Symfony)¶
Instale via Composer:
Exemplo de requisição 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);
?>
Exemplo de requisição 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. Notas e Melhores Práticas¶
- Substitua
YOUR_API_KEYpela sua chave real. - Utilize o parâmetro
langpara obter respostas em vários idiomas (en,fr,de,es, etc.). - Trate erros e exceções HTTP para garantir a confiabilidade da produção.
- Considere o uso de Guzzle ou Symfony HttpClient para aplicações avançadas devido à gestão de conexões, tentativas e suporte assíncrono.
- As respostas da API podem incluir sinais de risco, planos de ação e metadados
_enterprisequando disponíveis.
Próximos Passos¶
Verifique outros exemplos específicos para cada linguagem:
- Python
- JavaScript
- NodeJs
- Kotlin / Android
- Flutter / Dart
- React Native / Expo
- Bash Shell
- Go / Golang
- Ruby / Rails
Comece a integrar a API de Saúde e Fitness nas suas aplicações PHP hoje mesmo!
API de Saúde e Fitness Potencializando a infraestrutura digital de saúde moderna