<?php
$login_url = 'http://192.168.2.187/login';
$login_data = array(
'username' => 'admin',
'password' => 'admin'
);
// Make the login request with the data as a POST request
$login_context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => 'Content-Type: application/json',
'content' => json_encode($login_data)
)
));
$login_response = file_get_contents($login_url, false, $login_context);
$login_http_code = http_response_code();
// Check if login was successful
if ($login_http_code === 200) {
// If successful, retrieve the access token from the response
$login_result = json_decode($login_response, true);
if (isset($login_result['access_token'])) {
$access_token = $login_result['access_token'];
echo "Access token: " . $access_token . "\n";
$api_url = 'http://192.168.2.187/chargeSession';
// Make the API request with the access token as an Authorization header
$api_options = array(
'http' => array(
'method' => 'GET',
'header' => 'Authorization: Bearer ' . $access_token . "\r\n" .
'Content-Type: application/json'
)
);
$api_context = stream_context_create($api_options);
$api_response = file_get_contents($api_url, false, $api_context);
// Check if API request was successful
if ($api_response === false) {
echo "Failed to connect to API URL: $api_url";
} else {
$api_data = json_decode($api_response, true);
var_dump($api_data);
}
} else {
echo "Failed to retrieve access token";
}
} else {
// If unsuccessful, show error message
echo "Login failed: " . $login_response;
}
Hi @Rishabh_Singh
Can you add further details to the question? Also kindly make sure that the snippet does not contain any confidential details.