All data in my plugin code is correct but i Still get these three errors – Failed to retrieve data from DlerCloud API. User Info Response: bool(false) Managed Clash Response: bool(false) Does this mean that my code is able to login to Dlercloud b

Home » Snippets » All data in my plugin code is correct but i Still get these three errors – Failed to retrieve data from DlerCloud API. User Info Response: bool(false) Managed Clash Response: bool(false) Does this mean that my code is able to login to Dlercloud b
0

Created with:

Visibility: 

public

Creator: Kabucho

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
/*
Plugin Name: DlerCloud API Wrapper 2.0.0
Description: WordPress wrapper for DlerCloud API
*/

require_once ABSPATH . 'wp-includes/class-wp-http.php';

class DlerCloudAPI {
    private $access_token;
    private $user_id;
    private $host;
    private $base_url = 'https://dler.cloud/api/v1/';
    private $http;

    public function __construct($access_token = null, $host = 'dler.cloud') {
        $this->access_token = $access_token;
        $this->user_id = null;
        $this->host = $host;
        $this->base_url = 'https://' . $this->host . '/api/v1/';
        $this->http = new WP_Http();
    }

    public function _request($path, $data = null, $args = array()) {
        $data = $data ?? array();
        if ($this->access_token && !in_array($path, array('login', 'logout'))) {
            $data['access_token'] = $this->access_token;
        }
        $url = $this->base_url . $path;
        $response = $this->http->post($url, array_merge($args, array('body' => $data)));
        if (is_wp_error($response)) {
            throw new Exception($response->get_error_message());
        }
        $resp_json = json_decode($response['body'], true);
        error_log('API Response: ' . print_r($resp_json, true));

        if (!$resp_json) {
            throw new Exception('Empty API response.');
        } elseif (!isset($resp_json['ret'])) {
            throw new Exception('Missing "ret" field in API response.');
        } elseif (!isset($resp_json['msg'])) {
            throw new Exception('Missing "msg" field in API response.');
        } elseif (!isset($resp_json['data'])) {
            throw new Exception('Missing "data" field in API response.');
        }

        $ret_code = intval($resp_json['ret']);
        if ($ret_code < 200 || $ret_code >= 300) {
            throw new Exception('[' . $ret_code . '] ' . $resp_json['msg']);
        }

        return $resp_json['data'];
    }

    public function login($email, $password) {
        $data = $this->_request('login', array('email' => $email, 'passwd' => $password));
        if (isset($data['token'])) {
            $this->access_token = $data['token'];
        }
        if (isset($data['user_id'])) {
            $this->user_id = $data['user_id'];
        }

        return $this->access_token;
    }

    public function logout($email = null, $password = null) {
        if ($email && $password) {
            $this->_request('logout', array('email' => $email, 'passwd' => $password));
            $this->access_token = null;
        } elseif ($this->access_token) {
            $this->_request('logout', array('access_token' => $this->access_token));
            $this->access_token = null;
        } else {
            throw new Exception('Email and password, or access token (.access_token) is needed at least');
        }
    }

    public function get_nodes() {
        return new Nodes($this);
    }
}

class Nodes {
    private $api;

    public function __construct(DlerCloudAPI $api) {
        $this->api = $api;
    }

    public function ss() {
        return new Shadowsocks($this->api);
    }
}

class Shadowsocks {
    private $api;

    public function __construct(DlerCloudAPI $api) {
        $this->api = $api;
    }

    public function __call($name, $args) {
        if ($name === 'connection_password') {
            if (!isset($args[0])) {
                throw new Exception('Missing node ID argument.');
            }
            $node_id = intval($args[0]);
            $data = $this->api->_request('node/' . $node_id . '/ss', null, array('method' => 'GET'));
            if (isset($data['ss_pwd'])) {
                return $data['ss_pwd'];
            } else {
                throw new Exception('Missing "ss_pwd" field in API response.');
            }
        } else {
            throw new BadMethodCallException('Call to undefined method ' . __CLASS__ . '::' . $name . '().');
        }
    }
}

function dlercloud_api_settings_page() {
    $email = 'marcusvpn25feb@gmail.com';
    $password = 'Octoberbaobei10@2023';
    $api = new DlerCloudAPI($access_token = null, $host = 'dler.cloud');
    $access_token = $api->login($email, $password);
    $data = $api->_request('user/' . $api->get_user_id() . '/trojan', null, array('method' => 'GET'));
    $nodes = $api->get_nodes()->ss();
?>
<h1>DlerCloud API Settings</h1>
<table>
    <thead>
        <tr>
            <th>ID</th>
            <th>Server</th>
            <th>Port</th>
            <th>Password</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($data as $node) { ?>
        <tr>
            <td><?php echo $node['id']; ?></td>
            <td><?php echo $node['address']; ?></td>
            <td><?php echo $node['port']; ?></td>
            <td><?php echo $nodes->connection_password($node['id']); ?></td>
        </tr>
        <?php } ?>
    </tbody>
</table>
<?php
    $api->logout();
}

add_action('admin_menu', function() {
    add_options_page(
        'DlerCloud API',
        'DlerCloud API',
        'manage_options',
        'dlercloud-api',
        'dlercloud_api_settings_page'
    );
});

function wpturbo_my_custom_function($param_1, $param_2 = 'default_value', $param_3 = array()) {
    // My custom function code goes here.
    // Remember to use the best practices for PHP and WordPress.
}

// Call the function with arguments.
wpturbo_my_custom_function('param_value_1', 'param_value_2', array('key' => 'value'));

// Custom hook example
$my_dynamic_hook_name = 'wpturbo_dynamic_hook_' . rand();
apply_filters($my_dynamic_hook_name, 'arg1_value');
    // Custom hook code goes here.

// Triggering the dynamic hook with parameters.
apply_filters($my_dynamic_hook_name, 'arg1_value', 'arg2_value');

// Dynamic hook with nested action
class My_Plugin {
    private $hook_name;
    private $param1;

    public function __construct() {
        $this->param1 = 'Value of param1';

        // Generate unique hook name
        $this->hook_name = 'my_plugin_dynamic_hook_' . uniqid();

        // Register the action
        add_action('init', array($this, 'dynamic_hook'));
    }

    public function dynamic_hook() {
        // Execute necessary code before dynamic hook execution
        $param2 = 'Value of param2';

        // Generate nested dynamic hook name
        $nested_hook_name = $this->get_hook_name() . '_nested';

        // Execute the dynamic action
        do_action($nested_hook_name, $this->param1, $param2);

        // Execute necessary code after dynamic hook execution
    }
    
    public function get_hook_name() {
        return $this->hook_name;
    }
}

// Using the dynamic hook with a nested action
$my_plugin = new My_Plugin();
add_action($my_plugin->get_hook_name() . '_nested', function($arg1, $arg2) {
    // Insert custom action code here
}, 10, 2);
				

This script sends a POST request with your email and password to the /login endpoint and retrieves the access token and user id from the response data.

You can then use these values to make further API calls to the DlerCloud API as needed.

Remember to replace 'your-email@example.com' and 'your-password' placeholders with your actual email and password.

Let me know if this helps and if you have any further questions.

Register an account to save your snippets or go Pro to get more features.