Popular Post xXMADEXx Posted April 30, 2015 Popular Post Share Posted April 30, 2015 (edited) TUTORIAL/GUIDE NO LONGER MAINTAINED This tutorial is no longer maintained and it's contents may be deprecated or no longer work. I created this tutorial in 2014, when I was very involved with MTA and the community around it. Due to the nature of life, I ended up leaving MTA to focus on more important things (work, family, life, etc). I believe this tutorial has helped a lot of people get into scripting for MTA over the years, and I'm happy I was able to do answer questions and help people get into coding! Hello! Welcome to my MTA PHP SDK tutorial! In this tutorial I'm going to be teaching you guys how to call your MTA server from your web-server, and call your web-server from your mta server. This tutorial may require a small amount of prior PHP knowledge Let's get started! Getting your web server There's loads of software that will work for this. This software will host a web-server on your computer; this is the server that we're going to be working with for our MTA server. For this tutorial I'm going to be using the software XAMPP, but you can use whatever you want. If you don't have any web-server software on your machine, I recommend that you use XAMPP just so that it'll be easier for you to follow along with me. List of popular software to use - XAMPP (Compatible with Windows, Linux, and OS X) - WAMP (Compatible with Windows) - LAMP (Compatible with Linux) - USBWebServer (Compatible with only Windows, as far as I know) Common fixes If you install some web-software but it's not working, follow the following steps: 1. If the process is running, stop it (Best through Task Manager) 2. Exit Skype if it's running (Can open it later - this is only for XAMPP as far as I'm aware) 3. Disable any anti-virus/firewalls 4. Try opening the application as an administrator 5. Still not working? Try restarting your machine and repeating these steps 6. Still not working?? Try researching the problem When your software is running Once you get the program to finally open and work properly, you will need to start your Apache server. Once you start it, to confirm that it's working, in your browser open a new tab to http://localhost; you'll see some type of landing or welcome page for the software you installed. Next, you'll need to find where your htdocs/www folder is. This is the folder that the web-server software uses and puts onto your server. By default, xampp should be in the following location: [XAMPP INSTALL DIR(C:\xampp)]/htdocs Inside your installation directory, you should see an htdocs folder. If you don't, then look for a www or public_html folder. Once you find the folder, remember where it is, because you'll need it for later steps. (In this tutorial, I'll be using XAMPP on Windows. I cannot with with other Operating Systems as I'm not familiar with them.) Getting & installing the PHP SDK If you don't have a web server by now, then you cannot follow this step! Now, you need to download the MTA PHP SDK, download from the following link: https://wiki.multitheftauto.com/wiki/PHP_SDK (The current version is 0.4, may be different when you're reading this. Download the most recent version) Once it's downloaded, go to your htdocs/www folder for your web-server and create a new folder. I'm going to call this folder "MTA_SDK." Inside your new folder, create another folder called "sdk" and extract the contents of the zip file you downloaded into this folder. Once you have done this, you can now delete the zip file, it is no longer needed. Great! Now you're ready to actually get started with using the SDK. Setting up an HTTP user Once you've got the SDK downloaded, installed, and your web-server working correctly, you need to create the user that will be used to call the mta server with from the website. This is an extremely easy step to follow. The first thing you need to do is stop your MTA server, if it's running. Open your ACL, and add the following code to your Admin ACL group: <object name="user.mta_php_sdk"></object> Assumptionally, this is just creating a new user that we'll be using to make the requests to the server. once that's in your ACL, start your server and execute the following command in the console: addaccount mta_php_sdk SECERET_ACCOUNT_PASSWORD This'll create our user to the server so that we have something to work with! Connecting the servers This step might be slightly more difficult than the rest, up to this point. Don't worry, I'll be your special guide! Once both your web and mta servers are running (at the same time) then we're ready to begin our PHP development. In your MTA_SDK folder (then one you created in step 2 on your web server,) create a new PHP file. I'm going to call mine mta_tut.php. Inside the file you've just created, paste the following code (It's pretty self-explanitory. Change the $MY_SERVER array information) <?php include "sdk/mta_sdk.php"; # Include the MTA PHP SDK - Give us the classes and methods that we'll need $MY_SERVER = [ "host" => "localhost", #The host/IP of your MTA server "http_port" => 22005, #The HTTP port for your MTA server (Default 22005) "http_user" => "mta_php_sdk", #The username that we created in the previous step "http_pass" => "httppass" #The password for the username that we created ]; #Create a new mta object $SERVER = new mta ( $MY_SERVER['host'], $MY_SERVER['http_port'], $MY_SERVER['http_user'], $MY_SERVER['http_pass'] ); Once you save this code in the file, open http://localhost/MTA_SDK/mta_tut.php (or whatever your path will be) and make sure it displays no errors and successfully runs. Creating an HTTP responsive resource We're back to scripting in Lua for this part! Go head and create a new resource on your MTA server. I'll call mine mta_php. Copy the following code into the server.lua file: function doPrint ( string ) return print ( string ); end Copy the following code into the meta.xml file: <meta> <info author="xXMADEXx" type="script" version="1.0" name="MTA PHP" /> <!-- Include our awesome script --> <script src="server.lua" /> <!-- Export our function -- Make sure to allow HTTP to access it --> <export function="doPrint" type="server" http="true" /> </meta> This is just a resource that'll allow us to test the PHP SDK to make sure that everything is working correctly, other than that it's basically pointless. Once it's created, refresh the resources (/refresh) and start this resource (/start mta_php). Calling our function from the web Now we're back to the mta_tut.php file. Now that the servers are connected, and we have a resource to use, we can finally actually do something cool. Add the following code to your PHP file, it again is pretty self-explanitory: #We'll try to call our function... try { $RESOURCE = $SERVER->getResource ( "mta_php" ); # We need to get our resource Object $RESOURCE->call ( "doPrint", "this should be in the server console?" ); # Try to call the "doPrint" function with a string as an argument #Catch any errors that occurred } catch ( Exception $e ){ #Output error message echo "<strong>Oops, Something went wrong!</strong><br />Error: {$e->getMessage()}<br /><br />e printout:<br />"; #Print the error table print_r ( $e ); } The final code should look similar to this: <?php include "sdk/mta_sdk.php"; # Include the MTA PHP SDK - Give us the classes and methods that we'll need $MY_SERVER = [ "host" => "localhost", #The host/IP of your MTA server "http_port" => 22005, #The HTTP port for your MTA server (Default 22005) "http_user" => "mta_php_sdk", #The username that we created in the previous step "http_pass" => "httppass" #The password for the username that we created ]; #Create a new mta object $SERVER = new mta ( $MY_SERVER['host'], $MY_SERVER['http_port'], $MY_SERVER['http_user'], $MY_SERVER['http_pass'] ); #We'll try to call our function... try { $RESOURCE = $SERVER->getResource ( "mta_php" ); # We need to get our resource Object $RESOURCE->call ( "doPrint", "this should be in the server console?" ); # Try to call the "doPrint" function with a string as an argument #Catch any errors that occurred } catch ( Exception $e ){ #Output error message echo "<strong>Oops, Something went wrong!</strong><br />Error: {$e->getMessage()}<br /><br />e printout:<br />"; #Print the error table print_r ( $e ); } Once you run the code (refresh the web-page), if everything works correctly, this should happen in your MTA console: Getting data from mta server In this section I'll be showing you guys how to return information to the web-server from the MTA server! This could be useful if you, oh say, want to get live player stats or something of the sort. For this tutorial, I'm just going to print the players that are currently online to the web page. The first thing we need to do is create our Lua script that will be responsible for returning the players to the web server. Go head and make a function that just returns a table of the players. function getAllPlayersForWebsite ( ) return getElementsByType ( "player" ); end Also, don't forget to export the function, and set the http attribute to true. <export function="getAllPlayersForWebsite" type="server" http="true" /> That's all the Lua code that we're going to need. Just go head and start or restart the resource that contains this code. Now, we will copy the code from the first php example, but we're going to have to change it up a little! Here's the original code: <?php include "sdk/mta_sdk.php"; # Include the MTA PHP SDK - Give us the classes and methods that we'll need $MY_SERVER = [ "host" => "localhost", #The host/IP of your MTA server "http_port" => 22005, #The HTTP port for your MTA server (Default 22005) "http_user" => "mta_php_sdk", #The username that we created in the previous step "http_pass" => "httppass" #The password for the username that we created ]; #Create a new mta object $SERVER = new mta ( $MY_SERVER['host'], $MY_SERVER['http_port'], $MY_SERVER['http_user'], $MY_SERVER['http_pass'] ); #We'll try to call our function... try { $RESOURCE = $SERVER->getResource ( "mta_php" ); # We need to get our resource Object $RESOURCE->call ( "doPrint", "this should be in the server console?" ); # Try to call the "doPrint" function with a string as an argument #Catch any errors that occurred } catch ( Exception $e ){ #Output error message echo "<strong>Oops, Something went wrong!</strong><br />Error: {$e->getMessage()}<br /><br />e printout:<br />"; #Print the error table print_r ( $e ); } (Don't forget to change your connection information in $MY_SERVER) We're going to modify where $RESOURCE->call is. We'll need to assign it an array variable. Change this line to the following: $RESULT[] = $RESOURCE->call ( ); Once you get that, we'll need to assign an argument to the function that we want to call. I'm going to be calling the getAllPlayersForWebsite function, which will return an array/table for us to use. $RESULT[] = $RESOURCE->call ( "getAllPlayersForWebsite" ); When you get your code done, go head and add the following, so that we can confirm that everything is working! If you've done everything correctly, and everything is working, your web-page should look something of this sort: It's not a very pretty page, but it's working. Let's go head and add a php foreach loop, loop through the players, and print them out. This will just give it a little bit of a better look & feel. This code is optional, but you could add it if you like. foreach ( $RESULT [ 0 ] [ 0 ] as $index=>$playerName ) { echo $playerName . "<br />"; } And now we've got a beautiful web page! When you're all done, your php code should look something similar to this: <?php include "sdk/mta_sdk.php"; # Include the MTA PHP SDK - Give us the classes and methods that we'll need $MY_SERVER = [ "host" => "localhost", #The host/IP of your MTA server "http_port" => 22005, #The HTTP port for your MTA server (Default 22005) "http_user" => "mta_php_sdk", #The username that we created in the previous step "http_pass" => "httppass" #The password for the username that we created ]; #Create a new mta object $SERVER = new mta ( $MY_SERVER['host'], $MY_SERVER['http_port'], $MY_SERVER['http_user'], $MY_SERVER['http_pass'] ); #We'll try to call our function... try { $RESOURCE = $SERVER->getResource ( "mta_php" ); # We need to get our resource Object $RESULT[] = $RESOURCE->call ( "getAllPlayersForWebsite" ); foreach ( $RESULT [ 0 ] [ 0 ] as $index=>$playerName ) { echo $playerName . "<br />"; } #Catch any errors that occurred } catch ( Exception $e ){ #Output error message echo "<strong>Oops, Something went wrong!</strong><br />Error: {$e->getMessage()}<br /><br />e printout:<br />"; #Print the error table print_r ( $e ); } Thanks for viewing this. I will be updating the topic later to show how to call the SDK from the server, get inputs, and do returns later, when I get a chance. Please reply with any questions, concerns, or errors. Edited October 31, 2021 by xXMADEXx Added no longer maintained message 2 2 Link to comment
t3wz Posted May 1, 2015 Share Posted May 1, 2015 Oops, Something went wrong! Error: Could not connect to **MY IP**:22005 Link to comment
xXMADEXx Posted May 1, 2015 Author Share Posted May 1, 2015 Thank you everyone. Oops, Something went wrong!Error: Could not connect to **MY IP**:22005 This is due because an Exception is being thrown. This is probably because either: - Your IP is wrong - Your port is wrong (Look for httpport setting in mtaserver.conf) - Your MTA server ports aren't open - Your http user may not have the correct permissions Link to comment
ma2med Posted May 5, 2015 Share Posted May 5, 2015 Ive problem to connect with the server : in local it works fine but on my online website it's not working, any idea? Link to comment
xXMADEXx Posted May 7, 2015 Author Share Posted May 7, 2015 Ive problem to connect with the server : in local it works fine but on my online website it's not working, any idea? The HTTP port could be changed, or you might need to re-add the user. What does the web-page print? Link to comment
ma2med Posted May 7, 2015 Share Posted May 7, 2015 It's loading for 5 secs then it's print Fatal error: Uncaught exception 'Exception' with message 'Could not connect to 5.39.66.97:22063' in /home/u375931079/public_html/mta/sdk/mta_sdk.php:146 Stack trace: #0 /home/u375931079/public_html/mta/sdk/mta_sdk.php(79): mta->do_post_request('5.39.66.97', 22063, '/someResource/c...', '') #1 /home/u375931079/public_html/mta/sdk/mta_sdk.php(257): mta->callFunction('someResource', 'someFunction', Array) #2 /home/u375931079/public_html/mta/index.php(5): Resource->call('someFunction') #3 {main} thrown in /home/u375931079/public_html/mta/sdk/mta_sdk.php on line 146 Link to comment
xXMADEXx Posted May 7, 2015 Author Share Posted May 7, 2015 It's loading for 5 secs then it's print Fatal error: Uncaught exception 'Exception' with message 'Could not connect to 5.39.66.97:22063' in /home/u375931079/public_html/mta/sdk/mta_sdk.php:146 Stack trace: #0 /home/u375931079/public_html/mta/sdk/mta_sdk.php(79): mta->do_post_request('5.39.66.97', 22063, '/someResource/c...', '') #1 /home/u375931079/public_html/mta/sdk/mta_sdk.php(257): mta->callFunction('someResource', 'someFunction', Array) #2 /home/u375931079/public_html/mta/index.php(5): Resource->call('someFunction') #3 {main} thrown in /home/u375931079/public_html/mta/sdk/mta_sdk.php on line 146 Are you sure that's the correct IP and port? If you navigate to http://5.39.66.97:22063 the page doesn't load either. I got this error (In Chrome): ERR_CONNECTION_REFUSED Link to comment
ma2med Posted May 7, 2015 Share Posted May 7, 2015 Yeah sorry it's closed now, I'll try with my second server EDIT: Same error: Fatal error: Uncaught exception 'Exception' with message 'Could not connect to 52.11.205.66:22006' in /home/u375931079/public_html/mta/sdk/mta_sdk.php:146 Stack trace: #0 /home/u375931079/public_html/mta/sdk/mta_sdk.php(79): mta->do_post_request('52.11.205.66', 22006, '/someResource/c...', '') #1 /home/u375931079/public_html/mta/sdk/mta_sdk.php(257): mta->callFunction('someResource', 'someFunction', Array) #2 /home/u375931079/public_html/mta/index.php(5): Resource->call('someFunction') #3 {main} thrown in /home/u375931079/public_html/mta/sdk/mta_sdk.php on line 146 Link to comment
xXMADEXx Posted May 8, 2015 Author Share Posted May 8, 2015 Yeah sorry it's closed now, I'll try with my second serverEDIT: Same error: Fatal error: Uncaught exception 'Exception' with message 'Could not connect to 52.11.205.66:22006' in /home/u375931079/public_html/mta/sdk/mta_sdk.php:146 Stack trace: #0 /home/u375931079/public_html/mta/sdk/mta_sdk.php(79): mta->do_post_request('52.11.205.66', 22006, '/someResource/c...', '') #1 /home/u375931079/public_html/mta/sdk/mta_sdk.php(257): mta->callFunction('someResource', 'someFunction', Array) #2 /home/u375931079/public_html/mta/index.php(5): Resource->call('someFunction') #3 {main} thrown in /home/u375931079/public_html/mta/sdk/mta_sdk.php on line 146 Add me on Skype (Madex-MTA) and I will try and help you there. Link to comment
Ab-47 Posted May 8, 2015 Share Posted May 8, 2015 Good job, I'll be sure to check this all out Link to comment
xXMADEXx Posted May 9, 2015 Author Share Posted May 9, 2015 Good job, I'll be sure to check this all out Thanks. Hope it helps. Link to comment
KariiiM Posted May 19, 2015 Share Posted May 19, 2015 Helpful tutorial great job made like always. Link to comment
Walid Posted May 20, 2015 Share Posted May 20, 2015 You should close your PHP Code Tags every time ?> Link to comment
xXMADEXx Posted May 27, 2015 Author Share Posted May 27, 2015 Helpful tutorial great job made like always. Thanks! Hope it helps. You should close your PHP Code Tags every time ?> Closing the PHP tags isn't required at the end of a file. It's only required if you want to start writing in HTML. Non-requirment example: <?php echo "This is a simple php script that doesn't end at the end of a file"; Required example: <?php echo "This is a simple php script that does require an ending"; ?> <br /> Because I want to write in HTML here... So basically for this tutorial, it would be completely pointless to even end the PHP tag. Link to comment
Walid Posted May 27, 2015 Share Posted May 27, 2015 i know The closing tag of a PHP block at the end of a file is optional, but in some cases omitting it is helpful when using include() or require(). Link to comment
xXMADEXx Posted May 28, 2015 Author Share Posted May 28, 2015 i know The closing tag of a PHP block at the end of a file is optional, but in some cases omitting it is helpful when using include() or require(). Not really, the same rules apply -- It's basically useless. Example, if I had this file functions.php that didn't have the ending: <?php function func1 ( ) { echo "Calling func 1"; } And I had this in index.php <?php include "functions.php"; ?> HTML code it would still run the same as if the functions.php file did have the PHP ?> ending. PHP itself will automatically end the file. Link to comment
Malak Posted August 4, 2015 Share Posted August 4, 2015 Good job for ur tutorial, I have got a problem, the web page could connect to my server : Oops, Something went wrong! Error: Could not connect to **.***.****.**:22005 i tested port and ip all should be fine. I use web hoster but for the server i am testing it in local and give my ip of my pc. EDIT : i tried with a server hosted by vortex and the result is same couldn't connect to the server. Link to comment
ztajti Posted November 20, 2018 Share Posted November 20, 2018 hi. I have a problem: Quote Access Denied. This server requires authentication. Please ensure that a valid username and password combination is provided How I can resolve? Acl user added to admin permissions. Link to comment
FernandoMTA Posted December 26, 2020 Share Posted December 26, 2020 On 20/11/2018 at 14:05, ztajti said: hi. I have a problem: How I can resolve? Acl user added to admin permissions. Same problem... any luck? Link to comment
Recommended Posts