<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.multitheftauto.com/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Chaos</id>
	<title>Multi Theft Auto: Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.multitheftauto.com/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Chaos"/>
	<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/wiki/Special:Contributions/Chaos"/>
	<updated>2026-05-02T19:15:57Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.39.3</generator>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=PHP_SDK&amp;diff=62457</id>
		<title>PHP SDK</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=PHP_SDK&amp;diff=62457"/>
		<updated>2019-04-13T23:09:37Z</updated>

		<summary type="html">&lt;p&gt;Chaos: Added community python SDK&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;__NOTOC__&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
This SDK provides one function ''call'' that will allow you to call any exported script functions on any server that you have access to.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
==Functions==&lt;br /&gt;
===call===&lt;br /&gt;
This function calls an exported function in a specific resource.&lt;br /&gt;
&lt;br /&gt;
'''Syntax:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$mtaServer = new mta( $hostname, $port, $username, $password );&lt;br /&gt;
$resource = $mtaServer-&amp;gt;getResource ( $resourceName );&lt;br /&gt;
$returns[] = $resource-&amp;gt;call ( &amp;quot;functionName&amp;quot; [,args...] );&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===getInput===&lt;br /&gt;
This function is for use with web pages that are called by [[callRemote]].&lt;br /&gt;
&lt;br /&gt;
'''Syntax:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$inputData = mta::getInput();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===doReturn===&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Syntax:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
mta::doReturn( argument1, argument2 ... argumentN );&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
include( &amp;quot;mta_sdk.php&amp;quot; );&lt;br /&gt;
$mtaServer = new mta(&amp;quot;bastage.net&amp;quot;, 33004);&lt;br /&gt;
$resource = $mtaServer-&amp;gt;getResource ( &amp;quot;echobot&amp;quot; );&lt;br /&gt;
$retn = $resource-&amp;gt;call ( &amp;quot;getThisResource&amp;quot; ); // $retn is an array containing the values the function returned&lt;br /&gt;
$resourceElement = $retn[0]; // the first returned value is the resource&lt;br /&gt;
$retn = $resource-&amp;gt;call ( &amp;quot;getResourceName&amp;quot;, $resourceElement ); &lt;br /&gt;
$resourceName = $retn[0]; // contains the name of the resource 'echobot'&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Authentication==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
include( &amp;quot;mta_sdk.php&amp;quot; );&lt;br /&gt;
$mtaServer = new mta(&amp;quot;example.com&amp;quot;, 33004, &amp;quot;myUsername&amp;quot;, &amp;quot;myPassword&amp;quot; );&lt;br /&gt;
$mtaServer-&amp;gt;getResource(&amp;quot;someResource&amp;quot;)-&amp;gt;call(&amp;quot;someFunction&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A page that can be called by [[callRemote]]==&lt;br /&gt;
This example just adds two numbers passed to it by a lua script.&lt;br /&gt;
&lt;br /&gt;
'''PHP:''' (for the page that LUA expects to be at ''&amp;lt;nowiki&amp;gt;http://www.example.com/page.php&amp;lt;/nowiki&amp;gt;'')&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
include( &amp;quot;mta_sdk.php&amp;quot; );&lt;br /&gt;
$input = mta::getInput();&lt;br /&gt;
mta::doReturn($input[0] + $input[1]);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
'''LUA:'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
-- result is called when the function returns&lt;br /&gt;
function result(sum)&lt;br /&gt;
    outputChatBox(sum)&lt;br /&gt;
end&lt;br /&gt;
function addNumbers(number1, number2)&lt;br /&gt;
    callRemote ( &amp;quot;http://www.example.com/page.php&amp;quot;, result, number1, number2 )&lt;br /&gt;
end &lt;br /&gt;
addNumbers ( 123, 456 ) -- call the function&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Caveats==&lt;br /&gt;
* This only works with PHP 5.0 and above.&lt;br /&gt;
* You cannot currently compare two Element instances that you expect to be identical - you need to do a &amp;quot;deep compare&amp;quot;, comparing the &amp;quot;id&amp;quot; fields.&lt;br /&gt;
&lt;br /&gt;
==Download==&lt;br /&gt;
=== Note for version 0.4 ===&lt;br /&gt;
On line 80 in mta_sdk.php you will find the string:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
echo $json_output;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Replace that one with:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
//echo $json_output;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or remove it completly!&lt;br /&gt;
&lt;br /&gt;
=== Note for PHP 7.2.10 (and upwards) ===&lt;br /&gt;
&lt;br /&gt;
Methods with the same name as their class will not be constructors in a future version of PHP (7.0.x)+&lt;br /&gt;
&lt;br /&gt;
Using PHP 7.2.10 this seems to break completely&lt;br /&gt;
&lt;br /&gt;
On line 211 and 227 in mta_sdk.php find the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
line 211: class Element&lt;br /&gt;
line 227: class Resource&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Change these to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
line 211: class ElementClass&lt;br /&gt;
line 227: class ResourceClass&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now on line 42 and 99 find the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
line 42: $res = new Resource ( $resourceName, $this );&lt;br /&gt;
line 99: $item = new Element( substr( $item, 3 ) );&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Change these to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
line 42: $res = new ResourceClass ( $resourceName, $this );&lt;br /&gt;
line 99: $item = new ElementClass( substr( $item, 3 ) );&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now finally, on line 121, change this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
if ( get_class($item) == &amp;quot;Element&amp;quot; || get_class($item) == &amp;quot;Resource&amp;quot; )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
if ( get_class($item) == &amp;quot;ElementClass&amp;quot; || get_class($item) == &amp;quot;ResourceClass&amp;quot; )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All fixed!&lt;br /&gt;
&lt;br /&gt;
Visit [https://github.com/multitheftauto/mtasa-php-sdk/releases the releases page on GitHub] to download the SDK.&lt;br /&gt;
&lt;br /&gt;
=== Note for Python ===&lt;br /&gt;
&lt;br /&gt;
There is a community made Python SDK with the same semantics on [https://gitlab.com/OwlGamingCommunity/mta-python-sdk the OwlGaming Gitlab] for use on Python projects.&lt;br /&gt;
&lt;br /&gt;
== Version History ==&lt;br /&gt;
&lt;br /&gt;
* Version 0.4 - Renamed ''public function mta(..)'' to ''public function __construct(..)''.&lt;br /&gt;
* Version 0.3-fix - make call work with args&lt;br /&gt;
* Version 0.3 - Neater syntax, support functions for [[callRemote]] (this version is lost)&lt;br /&gt;
* Version 0.2 -  ''Deprecated - Syntax differs from examples shown above.'' - Adds authentication support.&lt;br /&gt;
* Version 0.1 - ''Deprecated - Syntax differs from examples shown above.''&lt;br /&gt;
&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Chaos</name></author>
	</entry>
</feed>