PHP code example of estaheri / 3x-ui

1. Go to this page and download the library: Download estaheri/3x-ui library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

estaheri / 3x-ui example snippets




$xui = new Xui($xui_host, $xui_port, $xui_path, $xui_ssl);

$xui->login($username, $password);

$xui->server; // Accessing to server methods (Status,Database,Stop/Restart Xray,...)
$xui->xray; // Accessing to xray methods (Inbounds,Outbounds,Routing,...)
$xui->panel; // Accessing to panel methods (Restart panel,Update/Get settings,...)

  $return = ['ok' => $ok, 'response' => $response, 'size' => $size, 'time_taken' => $time_taken]
  #---------------------------------------------------------------------------------
  $ok // Can be true/false based on request success or fail! (bool)
  $response // response of request returned by panel (string|object|array)
  $size // Size of response (int)
  $time_taken // Request time taken in seconds (float)
  

  $response = ['success' => $success, 'obj' => $obj, 'msg' => $msg]
  #---------------------------------------------------------------------------------
  $success // Can be true/false based on action success or fail! (bool)
  $obj // data returned. (string|object|array)
  $msg // Message for fail actions, Similar to error! (string)
  

// Create Inbound config
use XUI\Xray\Inbound\Protocols\Vmess\Vmess;
$listen = '';
$port = 12345;
$config = new Vmess($listen, $port);
$config->settings->add_client($enable, $uuid, $email, $total_traffic, $expiry_time, $limit_ip, $tgid, $subid, $reset);
$config->stream_settings->ws_settings($accept_proxy_protocol,$path,$headers);
#---------------------------------------------------------------------------------
// Create Outbound config
use XUI\Xray\Outbound\Protocols\Vmess\Vmess;
$config = new Vmess();
$config->settings->address = 'example.3xui.net';
$config->settings->port = 12345;
$config->settings->add_user($uuid, $security);
$config->stream_settings->ws_settings($accept_proxy_protocol, $path, $headers);

use XUI\Xray\Routing\Rule;
$rule = new Rule($inbound_tag,$outbound_tag);
// Or
$rule = Routing::rule($inbound_tag,$outbound_tag);

$rule->port($value); // return true on success and false on failure
$port = $rule->port();

$rule->balancer_tag(); // Corresponds to the identifier of a balancer.
$rule->user(); // An array where each item represents an email address.
$rule->network(); // This can be "tcp", "udp", or "tcp,udp".
$rule->protocol(); // An array where each item represents a protocol. ["http" | "tls" | "bittorrent"]
$rule->domain_matcher(); // The domain matching algorithm used varies depending on the settings.
$rule->domain(); // The domain matching algorithm used varies depending on the settings.
$rule->ip(); // An array where each item represents an IP range.
$rule->port(); // The target port range
$rule->source(); // An array where each item represents an IP range in the format of IP, CIDR, GeoIP, or loading IP from a file.
$rule->source_port(); // The source port
$rule->attrs(); // A json object with string keys and values, used to detect the HTTP headers of the traffic.
$rule->type(); // Currently, only the option "field" is supported.

$xui = new \XUI\Xui($host, $port, $uri_path, $has_ssl, $cookie_dir, $timeout, $proxy, $output, $response_output);
#---------------------------------------------------------------------------------
$host = 'localhost'; // Host address of 3x-ui panel. (Accepts Domain/Subdomain/Ipv4)
$port = 12345; // Port of 3x-ui panel. (1-65535)
$uri_path = '/'; // URI path of 3x-ui panel.
$has_ssl = false; // Does panel has SSL. (Default: FALSE)
$cookie_dir = __DIR__ . '/.cookie'; //
$timeout = 10; // HTTP Requests timeout
$proxy = null; // HTTP Requests proxy
$output = \XUI\Xui::OUTPUT_OBJECT; // Type of return value of methods. Use Xui::OUTPUT_xxx to set. (Accepts json,object,array)
$response_output = \XUI\Xui::OUTPUT_OBJECT; // Type of response value of requests. Use Xui::OUTPUT_xxx to set. (Accepts json,object,array)

  $xui->login($username,$password);
  #---------------------------------------------------------------------------------
  $username = 'admin'; // Panel login username
  $password = 'xxxx'; // Panel login password
  

    \XUI\Xui::random($length);
    $length = 32; // Length of random string
    

    \XUI\Xui::uuid();
    

$xray = $xui->xray;

    $inbound = $xray->inbound;
    

    # Add,Delete,Update,Get,Exist inbound
    $inbound->add($config, $remark, $total_traffic, $expiry_time, $download, $upload, $enable);
    $inbound->exist($inbound_id);
    $inbound->get($inbound_id);
    $inbound->update($inbound_id, $config, $remark, $total_traffic, $expiry_time, $download, $upload, $enable);
    $inbound->delete($inbound_id);
    # List,Online inbounds
    $inbound->onlines();
    $inbound->list();
    # Import,Export inbound
    $inbound->export($inbound_id);
    $inbound->import($exported_inbound);
    # Get,Clear client ips of inbound
    $inbound->get_client_ips($client_email);
    $inbound->clear_client_ips($client_email);
    $inbound->reset_client_traffic($inbound_id, $client_email);
    #---------------------------------------------------------------------------------
    $config = new \XUI\Xray\Inbound\Protocols\Vmess\Vmess(); // Configured protocol object oriented class 
    $config->settings->add_client();
    $config->stream_settings->ws_settings(false, '/');
    $inbound_id = 123; // ID of inbound
    $remark = 'Me'; // Name of inbound
    $total_traffic = 100 * \XUI\Xui::UNIT_GIGABYTE; // Total traffic of inbound. (Unit: Byte)
    $download = 10 * \XUI\Xui::UNIT_GIGABYTE; // Download traffic usage of inbound. (Unit: Byte)
    $upload = 500 * \XUI\Xui::UNIT_MEGABYTE; // Upload traffic usage of inbound. (Unit: Byte)
    $enable = true; // Enable/Disable inbound
    $expiry_time = time() + (30 * 86400); // Expiry time of inbound. (Unit: unix timestamp in seconds)
    $exported_inbound = 'json'; // Json encoded exported inbound.
    $client_email = 'client1234@localhost'; // Client email on inbound
    

    $outbound = $xray->outbound;
    

    # Add,Delete,Update,Get,Exist outbound
    $outbound->add($tag,$config,$proxy_settings,$send_through,$mux);
    $outbound->exist($outbound_tag);
    $outbound->get($outbound_tag);
    $outbound->update($outbound_tag, $tag, $config, $proxy_settings, $send_through, $mux);
    $outbound->delete($outbound_tag);
    # List outbound
    $outbound->list();
    #---------------------------------------------------------------------------------
    $config = new \XUI\Xray\Outbound\Protocols\Vmess\Vmess(); // Configured protocol object oriented class 
    $config->settings->add_user(\XUI\Xui::uuid());
    $config->stream_settings->ws_settings(false);
    $tag = 'vmess-test'; // The identifier of this outbound connection
    $proxy_settings = null; // The outbound proxy configuration.
    $send_through = '0.0.0.0'; // The IP address used to send data.
    $mux = []; // Specific configuration related to Mux.
    $outbound_tag = 'vmess-test'; // The identifier of this outbound connection
    

    $routing = $xray->routing;
    $loaded = $routing->load();
    if($loaded)
        echo "ok";
    else
        echo 'error';
    

  # Set/Get routing domain strategy,domain matcher,balancers
  $routing->domain_strategy();
  $routing->domain_matcher();
  $routing->balancers();
  # Add,Delete,Update,Get,Exist routing rule
  $routing->has_rule($rule_inbound_tag,$rule_outbound_tag);
  $routing->add_rule($rule,$apply);
  $routing->get_rule($rule_inbound_tag,$rule_outbound_tag);
  $routing->update_rule($rule_inbound_tag,$rule_outbound_tag,$rule,$apply);
  $routing->delete_rule($rule_inbound_tag,$rule_outbound_tag,$apply);
  # Apply changes made to routing
  $routing->update();
  #---------------------------------------------------------------------------------
  $rule_inbound_tag = ['inbound-12345','inbound-12346']; // An array where each item represents an identifier.
  $rule_outbound_tag = 'direct'; // Corresponds to the identifier of an outbound.
  $apply = true; // Apply changes to routing in xray config
  $rule = \XUI\Xray\Routing\Routing::rule($inbound_tag,$outbound_tag); // Configured rule object oriented class
  

    $reverse = $xray->reverse;
    $loaded = $reverse->load();
    if($loaded)
        echo "ok";
    else
        echo 'error';
    

  # Add,Delete,Update,Get,Exist reverse portal
  $reverse->has_portal($portal_tag);
  $reverse->add_portal($tag,$domain,$apply);
  $reverse->get_portal($portal_tag);
  $reverse->update_portal($portal_tag,$tag,$domain,$apply);
  $reverse->delete_portal($portal_tag,$apply);
  # Add,Delete,Update,Get,Exist reverse bridge
  $reverse->has_bridge($bridge_tag);
  $reverse->add_bridge($tag,$domain,$apply);
  $reverse->get_bridge($bridge_tag);
  $reverse->update_bridge($bridge_tag,$tag,$domain,$apply);
  $reverse->delete_bridge($bridge_tag,$apply);
  # Apply changes made to reverse
  $reverse->update();
  #---------------------------------------------------------------------------------
  $portal_tag = 'portal-1'; // The identifier for the portal
  $bridge_tag = 'bridge-1'; // The identifier for the bridge
  $tag = 'portal-1'; // The identifier for the portal/bridge
  $domain = 'reverse.xui'; // A domain name.
  $apply = true; // Apply changes to reverse in xray config
  

  # Get full Xray configs
  $xray->get_configs();
  # Get/Update a Xray config/configs
  $xray->get_config($config);
  $xray->update_config($update);
  # Set a full xray configuration
  $xray->set_config($full_config);
  # Restart xray core to apply changes made to xray config
  $xray->restart();
  # Get inbound tags
  $xray->get_inbound_tags();
  #---------------------------------------------------------------------------------
  $config = 'log'; // Configuration/Configurations you want to get
  $update = [
      'log' => [
          'access' => 'none',
          'dnsLog' => false,
          'error' => '',
          'loglevel' => 'warning',
          'maskAddress' => '',
      ]
  ]; // Configuration/Configurations you want to made to xray configs
  $full_config = 'json'; // Json/object/array of xray full config
  

$server = $xui->server;

    $server->status();
    

    $server->get_db($path);
    $server->import_db($path_or_db);
    #---------------------------------------------------------------------------------
    $path = '/www/wwwroot/xui.example.com/x-ui.db'; // Path to .db file for exporting panel database
    $path_or_db = '/www/wwwroot/xui.example.com/x-ui.db'; // Path to .db file of exported panel database
    

    $server->restart_xray();
    $server->stop_xray();
    

    $server->get_x25519_cert();
    

    $server->get_xray_config();
    

    $server->get_xui_log($count, $level, $syslog);
    #---------------------------------------------------------------------------------
    $count = 10; // Count of logs
    $level = 'notice'; // Logs level (debug,info,notice,warning,error)
    $syslog = true; // Enable/Disable syslog output
    

    # Get panel full settings
    $panel->settings();
    # Get a setting/ settings from panel settings
    $panel->get_setting($setting);
    # Update panel settings
    $panel->update_setting($update);
    #---------------------------------------------------------------------------------
    $setting = 'webPort'; // Specified setting you want to get
    $update = ['webPort'=>1234]; // Changes you want to made to Panel settings 
    

    $panel->restart();
    

    $panel->default_xray_config();