0
X
Add Snippet To Project
New Project
Add To Existing Project
<?php
/*
Plugin Name: DlerCloud API Wrapper 1.0.1
Description: WordPress wrapper for DlerCloud API
*/
// Include the WP_Http class
require_once ABSPATH . 'wp-includes/class-wp-http.php';
// DlerCloud API class
class DlerCloudAPI {
// Properties
private $access_token;
private $base_url;
private $http;
// Constructor
public function __construct($access_token = null) {
$this->access_token = $access_token;
$this->base_url = 'https://dler.cloud/api/v1/';
$this->http = new WP_Http();
}
// Method to make API requests
private function request($endpoint, $method = 'POST', $data = array()) {
$url = $this->base_url . $endpoint;
$headers = array(
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $this->access_token
);
$args = array(
'headers' => $headers,
'method' => $method,
'timeout' => 30, // API request timeout, in seconds
'body' => json_encode($data)
);
$response = $this->http->request($url, $args);
if (is_wp_error($response)) {
return false; // return false if there was a problem with the API request
}
$body = wp_remote_retrieve_body($response);
$decoded_body = json_decode($body, true);
if ($decoded_body && isset($decoded_body['ret']) && $decoded_body['ret'] === 200) {
return $decoded_body['data'] ?? array(); // return the response data if successful
}
return false; // return false if the API request was not successful
}
// Method to log in to the DlerCloud API
public function login($email, $password) {
$data = array(
'email' => $email,
'passwd' => $password
);
$response = $this->request('login', 'POST', $data);
if ($response && isset($response['token'])) {
$this->access_token = $response['token'];
return true; // return true if login was successful and access token was obtained
}
return false; // return false if login was unsuccessful
}
// Method to retrieve user information from the DlerCloud API
public function get_user_info() {
return $this->request('information');
}
// Method to retrieve managed Clash data from the DlerCloud API
public function get_managed_clash() {
return $this->request('managed/clash');
}
}
// Plugin Settings Page
function dlercloud_api_settings_page() {
$email = 'marcusvpn25feb@gmail.com'; // Replace with your DlerCloud email
$password = 'Octoberbaobei10@2023'; // Replace with your DlerCloud password
$api = new DlerCloudAPI();
// Log in to obtain the access token
$logged_in = $api->login($email, $password);
if ($logged_in) {
// Retrieve user information
$user_info = $api->get_user_info();
// Retrieve managed Clash data
$managed_clash = $api->get_managed_clash();
if ($user_info !== false && $managed_clash !== false) {
// Rest of the code remains the same
// ...
} else {
// If either request returned false, display an error message and the response data
echo 'Failed to retrieve data from DlerCloud API.';
echo '<br>';
echo 'User Info Response: ';
var_dump($user_info);
echo '<br>';
echo 'Managed Clash Response: ';
var_dump($managed_clash);
}
} else {
echo 'Failed to log in to DlerCloud API.'; // Display an error message if login was unsuccessful
}
}
// Add the settings page to WordPress admin menu
function dlercloud_api_add_menu() {
add_options_page('DlerCloud API Settings', 'DlerCloud API', 'manage_options', 'dlercloud-api-settings', 'dlercloud_api_settings_page');
}
// Hook the menu function to the admin menu
add_action('admin_menu', 'dlercloud_api_add_menu');
I hope the comments are helpful!