Migrated from Redmine #1214 | Author: Michael Bay
Status: Feedback | Priority: High, I’m very impatient | Created: 2024-04-15
Hi there hope you guys can help me. I’m a bit confused ![]()
I’m trying to create a custom WordPress plugin to show data from Simplybook.me trough API
I really struggle to understand this “company login” and to list services
Have tried multiple things on this API explorer: API documentation | SimplyBook.me Online Scheduling
And i get this “Company dosn’t exist” no matter what I try.
If I add “742” and the API key from cluster dashboard, then i get “null” results, and can get to the page where I can choose service, and provider (but nothing there)
If I add “741” I get error company doesn’t excist.
!explorer.jpg!
Right now, I’m using this code. Actually without anything with “company_login”
The API key is from the cluster dashboard, where I got 2 listed companies.
zencyapsArea1 = id 741
zencyapsArea2 = id 742
If I go into the page “Custom Features” and enable API, and use them, I get AUTH error. So only thing I can connect with, is the Cluster API key
<?php
$api_key = 'API KEY FROM CLUSTER DASHBOARD';
// Authenticate to get the token
$auth_url = "https://cluster-api.simplybook.pro/auth";
$auth_response = wp_remote_post($auth_url, array(
'headers' => array(
'Content-Type' => 'application/json',
'X-Cluster' => 'zencyaps'
),
'body' => json_encode(array(
'key' => $api_key
))
));
if (is_wp_error($auth_response)) {
echo 'Error authenticating: ' . $auth_response->get_error_message();
exit;
}
$auth_body = wp_remote_retrieve_body($auth_response);
$auth_result = json_decode($auth_body, true);
if (!$auth_result || !isset($auth_result['token'])) {
echo 'Error: Unable to authenticate. Please check your API key.';
exit;
}
// Extract token from authentication response
$token = $auth_result['token'];
// Fetch companies list
$companies_url = "https://cluster-api.simplybook.pro/companies?filter[status]=active";
$companies_response = wp_remote_get($companies_url, array(
'headers' => array(
'Content-Type' => 'application/json',
'X-Cluster' => 'zencyaps',
'X-Token' => $token
)
));
if (is_wp_error($companies_response)) {
echo 'Error fetching companies: ' . $companies_response->get_error_message();
exit;
}
$companies_body = wp_remote_retrieve_body($companies_response);
$companies_result = json_decode($companies_body, true);
if (!$companies_result || empty($companies_result['data'])) {
echo 'Error: No active companies found.';
exit;
}
// Get company service account API token
$company_login = $companies_result['data'][0]['login']; // Assuming the first company in the list
$service_token_url = "https://cluster-api.simplybook.pro/companies/{$company_login}/api-token";
$service_token_response = wp_remote_post($service_token_url, array(
'headers' => array(
'Content-Type' => 'application/json',
'X-Cluster' => 'zencyaps',
'X-Token' => $token
)
));
if (is_wp_error($service_token_response)) {
echo 'Error getting service token: ' . $service_token_response->get_error_message();
exit;
}
$service_token_body = wp_remote_retrieve_body($service_token_response);
$service_token_result = json_decode($service_token_body, true);
if (!$service_token_result || !isset($service_token_result['token'])) {
echo 'Error: Unable to get service token.';
exit;
}
// Extract service token
$service_token = $service_token_result['token'];
// Now, use the service token to fetch client bookings
$client_bookings_url = "https://cluster-api.simplybook.pro/getClientBookingsDefault";
$client_bookings_response = wp_remote_post($client_bookings_url, array(
'headers' => array(
'Content-Type' => 'application/json',
'X-Cluster' => 'zencyaps',
'X-Token' => $service_token
),
'body' => json_encode(array(
'company_id' => $company_login,
'start_date' => date('Y-m-d'), // Change this to adjust the start date if needed
'end_date' => date('Y-m-d', strtotime('+1 month')) // Change this to adjust the end date if needed
))
));
if (is_wp_error($client_bookings_response)) {
echo 'Error fetching client bookings: ' . $client_bookings_response->get_error_message();
exit;
}
$client_bookings_body = wp_remote_retrieve_body($client_bookings_response);
$client_bookings_result = json_decode($client_bookings_body, true);
// Display client bookings
if ($client_bookings_result && isset($client_bookings_result['bookings'])) {
$client_bookings = $client_bookings_result['bookings'];
echo 'Client Bookings
';
echo '';
foreach ($client_bookings as $client_booking) {
echo "- Booking ID: {$client_booking['id']}, Start Time: {$client_booking['start_time']}, End Time: {$client_booking['end_time']}
";
}
echo '
';
} else {
echo 'No client bookings received from API.';
}
?>
And then I’m getting this “No client bookings received from API.”
There seems to be a connection, because if I type in another key, then it gives an AUTH error