In the following process we now need the two keys, Client and Secret, which were generated in the API example part 1 by adding an API access through the TSI. Furthermore, a suitable source code editor, such as Notepad++ or PhpStorm would be advantageous for the following steps, even if you could theoretically use a normal editor.
As mentioned in the title below, we use PHP as the programming language and the PHP extension cURL to communicate with the Teamspeak interface via the interface. In order to make it as easy as possible for beginners, I also use a predefined class called "curl / curl", which should make your work easier.
To access the class "Curl" as in the following script, visit the website https://github.com/php-mod/curl, download the class and copy all files into any subfolder, in this example use I use "curl" as the directory name.
// Include class curl / curl
$curl = new \Curl\Curl();
// If you also connect via the HTTPS protocol and you get errors,
// you should first try to activate the following options. Just remove the two
// Slashes at the beginning of each line.
// $curl->setOpt(CURLOPT_RETURNTRANSFER, TRUE); // (optional)
// $curl->setOpt(CURLOPT_SSL_VERIFYPEER, FALSE); // (optional)
// Store client key in variable
$client = "JDJ5JDEwJDJpeTN4bzFKWFNldmhBbXJVRWFLWU9oNXJZUzU1MU4zZlZJWGYuaXpXWmVFWVFGaG5zSHFD";
// Store secret key in variable
$secret = "JDJ5JDEwJFNLUGE4VnFCMmdyeHlnOGNSVzJmUS5kdy9FZVowR0pkMXFIZ1ZkbXhhOHNLUDN0akF1YmlL";
// Here we check if the connection could be made successfully with cURL and if not we issue the error with echo
echo "cURL Error: " . $curl->error_message;
// If the connection using cURL was successful, we check whether the interface accepts the request.
// To do this we convert the response string into an array using json_decode and check if the field
// "valid" is true, if there is not an error, which we output by means of echo and the field "error".
$response = json_decode($curl->response, true);
// If successful, the return value here should be the string "access granted"
// If the authentication fails, the error message is displayed here
echo "API Error: " . $response["error"];
I hope that the example shown above provides a reasonable understanding of how the interface works. If there are any questions, just leave a comment on this article. In the next part, I will show you an example of how to add a new TSI user.