Difference between revisions of "PHP SDK"

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
m (Alter previous change)
Line 132: Line 132:
<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
line 121: if ( get_class($item) == "Element" || get_class($item) == "Resource" )
if ( get_class($item) == "Element" || get_class($item) == "Resource" )
Line 138: Line 138:
<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
line 121: if ( get_class($item) == "ElementClass" || get_class($item) == "ResourceClass" )
if ( get_class($item) == "ElementClass" || get_class($item) == "ResourceClass" )

Revision as of 17:12, 8 January 2019

You can access the MTA Web Interface from almost any programming language that can request web pages using HTTP POST and encode and decode JSON. PHP can do this very easily.

This SDK provides one function call that will allow you to call any exported script functions on any server that you have access to.

The download below includes two example pages - one that shows a simple scoreboard, the other that shows the automatic handling of element and resource objects.

If you use this and make any improvements, feel free to send us a copy and we can host it for others to benefit from.



This function calls an exported function in a specific resource.


$mtaServer = new mta( $hostname, $port, $username, $password );
$resource = $mtaServer->getResource ( $resourceName );
$returns[] = $resource->call ( "functionName" [,args...] );


This function is for use with web pages that are called by callRemote.


$inputData = mta::getInput();


Use this function when you want to return data when a page is called with callRemote. You should NOT output any other data to the page, e.g. using echo as this will cause the return to fail.


mta::doReturn( argument1, argument2 ... argumentN );


include( "mta_sdk.php" );
$mtaServer = new mta("bastage.net", 33004);
$resource = $mtaServer->getResource ( "echobot" );
$retn = $resource->call ( "getThisResource" ); // $retn is an array containing the values the function returned
$resourceElement = $retn[0]; // the first returned value is the resource
$retn = $resource->call ( "getResourceName", $resourceElement ); 
$resourceName = $retn[0]; // contains the name of the resource 'echobot'


If the server you are accessing requires authentication, you must pass the http_username and http_password variables to your instantiated instance of the mta() class.


include( "mta_sdk.php" );
$mtaServer = new mta("example.com", 33004, "myUsername", "myPassword" );

A page that can be called by callRemote

This example just adds two numbers passed to it by a lua script.

PHP: (for the page that LUA expects to be at http://www.example.com/page.php)

include( "mta_sdk.php" );
$input = mta::getInput();
mta::doReturn($input[0] + $input[1]);


-- result is called when the function returns
function result(sum)
function addNumbers(number1, number2)
    callRemote ( "http://www.example.com/page.php", result, number1, number2 )
addNumbers ( 123, 456 ) -- call the function


  • This only works with PHP 5.0 and above.
  • You cannot currently compare two Element instances that you expect to be identical - you need to do a "deep compare", comparing the "id" fields.


Note for version 0.4

On line 80 in mta_sdk.php you will find the string:

echo $json_output;

Replace that one with:

//echo $json_output;

Or remove it completly!

Note for PHP 7.2.10 (and upwards)

Methods with the same name as their class will not be constructors in a future version of PHP (7.0.x)+

Using PHP 7.2.10 this seems to break completely

On line 211 and 227 in mta_sdk.php find the following:

line 211: class Element
line 227: class Resource

Change these to:

line 211: class ElementClass
line 227: class ResourceClass

Now on line 42 and 99 find the following:

line 42: $res = new Resource ( $resourceName, $this );
line 99: $item = new Element( substr( $item, 3 ) );

Change these to:

line 42: $res = new ResourceClass ( $resourceName, $this );
line 99: $item = new ElementClass( substr( $item, 3 ) );

Now finally, on line 121, change this:

if ( get_class($item) == "Element" || get_class($item) == "Resource" )

To this:

if ( get_class($item) == "ElementClass" || get_class($item) == "ResourceClass" )

All fixed!