|
| 1 | +# First Steps |
| 2 | + |
| 3 | +This guide explains how to set up and use the TeamSpeak PHP framework in a new or existing PHP project. |
| 4 | + |
| 5 | +## Example Project Structure |
| 6 | + |
| 7 | +The following example shows a simple blank PHP project: |
| 8 | + |
| 9 | +```shell |
| 10 | +. |
| 11 | +├── autoload.php |
| 12 | +├── index.php |
| 13 | +└── src |
| 14 | +``` |
| 15 | + |
| 16 | +Inside the `src` directory, place the required packages: |
| 17 | + |
| 18 | +```shell |
| 19 | +. |
| 20 | +├── phpseclib |
| 21 | +└── ts3phpframework |
| 22 | +``` |
| 23 | + |
| 24 | +The complete structure should look like this: |
| 25 | + |
| 26 | +```shell |
| 27 | +. |
| 28 | +├── autoload.php |
| 29 | +├── index.php |
| 30 | +└── src |
| 31 | + ├── phpseclib |
| 32 | + └── ts3phpframework |
| 33 | +``` |
| 34 | + |
| 35 | +## Setting Up the Autoloader |
| 36 | + |
| 37 | +Create an `autoload.php` file in your project root. |
| 38 | + |
| 39 | +This autoloader maps PHP namespaces to the corresponding source directories. |
| 40 | +It is flexible enough to support additional packages if required. |
| 41 | + |
| 42 | +```php |
| 43 | +<?php |
| 44 | + |
| 45 | +spl_autoload_register(function ($class) { |
| 46 | + $prefixes = array( |
| 47 | + 'PlanetTeamSpeak\\TeamSpeak3Framework\\' => __DIR__ . '/src/ts3phpframework/', |
| 48 | + 'phpseclib3\\' => __DIR__ . '/src/phpseclib/', |
| 49 | + ); |
| 50 | + |
| 51 | + foreach ($prefixes as $prefix => $baseDir) { |
| 52 | + $prefixLength = strlen($prefix); |
| 53 | + |
| 54 | + if (strncmp($prefix, $class, $prefixLength) !== 0) { |
| 55 | + continue; |
| 56 | + } |
| 57 | + |
| 58 | + $relativeClass = substr($class, $prefixLength); |
| 59 | + $file = $baseDir . str_replace('\\', '/', $relativeClass) . '.php'; |
| 60 | + |
| 61 | + if (file_exists($file)) { |
| 62 | + require $file; |
| 63 | + } |
| 64 | + |
| 65 | + return; |
| 66 | + } |
| 67 | +}); |
| 68 | +``` |
| 69 | + |
| 70 | +If you need to add more libraries later, extend the `$prefixes` array with another namespace-to-directory mapping. |
| 71 | + |
| 72 | +Example: |
| 73 | + |
| 74 | +```php |
| 75 | +<?php |
| 76 | + |
| 77 | +$prefixes = array( |
| 78 | + 'PlanetTeamSpeak\\TeamSpeak3Framework\\' => __DIR__ . '/src/ts3phpframework/', |
| 79 | + 'phpseclib3\\' => __DIR__ . '/src/phpseclib/', |
| 80 | + 'Vendor\\Package\\' => __DIR__ . '/src/package/', |
| 81 | +); |
| 82 | +``` |
| 83 | + |
| 84 | +## Creating the Entry File |
| 85 | + |
| 86 | +Create an `index.php` file in your project root. |
| 87 | +This file includes the autoloader and creates a connection to the TeamSpeak server. |
| 88 | + |
| 89 | +```php |
| 90 | +<?php |
| 91 | + |
| 92 | +require_once __DIR__ . '/autoload.php'; |
| 93 | + |
| 94 | +use PlanetTeamSpeak\TeamSpeak3Framework\TeamSpeak3; |
| 95 | + |
| 96 | +$TN = array( |
| 97 | + 'serveradmin' => 'serveradmin', |
| 98 | + 'serverpassword' => 'MYPASSWD', |
| 99 | + 'serverhost' => '127.0.0.1', |
| 100 | + 'serverport' => '10022' |
| 101 | +); |
| 102 | + |
| 103 | +$TS = array( |
| 104 | + 'serverport' => '9987' |
| 105 | +); |
| 106 | + |
| 107 | +try { |
| 108 | + $ts3 = TeamSpeak3::factory( |
| 109 | + 'serverquery://' |
| 110 | + . $TN['serveradmin'] |
| 111 | + . ':' |
| 112 | + . $TN['serverpassword'] |
| 113 | + . '@' |
| 114 | + . $TN['serverhost'] |
| 115 | + . ':' |
| 116 | + . $TN['serverport'] |
| 117 | + . '/?server_port=' |
| 118 | + . $TS['serverport'] |
| 119 | + . '&use_offline_as_virtual=1' |
| 120 | + . '&no_query_clients=1' |
| 121 | + ); |
| 122 | + |
| 123 | + $nodeInfo = $ts3->getInfo(); |
| 124 | + echo $nodeInfo['virtualserver_platform']; |
| 125 | + |
| 126 | +} catch (Exception $e) { |
| 127 | + echo $e->getMessage(); |
| 128 | +} |
| 129 | +``` |
0 commit comments