PHP SDK: Difference between revisions
mNo edit summary |
No edit summary |
||
Line 11: | Line 11: | ||
'''Syntax:''' | '''Syntax:''' | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua" lang="php"> | ||
$mtaServer = new mta( $hostname, $port, $username, $password ); | $mtaServer = new mta( $hostname, $port, $username, $password ); | ||
$resource = $mtaServer->getResource ( $resourceName ); | $resource = $mtaServer->getResource ( $resourceName ); | ||
Line 21: | Line 21: | ||
'''Syntax:''' | '''Syntax:''' | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua" lang="php"> | ||
inputData[] = mta::getInput(); | inputData[] = mta::getInput(); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 29: | Line 29: | ||
'''Syntax:''' | '''Syntax:''' | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua" lang="php"> | ||
mta::doReturn( argument1, argument2 ... argumentN ); | mta::doReturn( argument1, argument2 ... argumentN ); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==Examples== | ==Examples== | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua" lang="php"> | ||
include( "mta_sdk.php" ); | include( "mta_sdk.php" ); | ||
$mtaServer = new mta("bastage.net", 33004); | $mtaServer = new mta("bastage.net", 33004); | ||
Line 48: | Line 48: | ||
'''Example:''' | '''Example:''' | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua" lang="php"> | ||
include( "mta_sdk.php" ); | include( "mta_sdk.php" ); | ||
$mtaServer = new mta("example.com", 33004, "myUsername", "myPassword" ); | $mtaServer = new mta("example.com", 33004, "myUsername", "myPassword" ); | ||
Line 58: | Line 58: | ||
'''PHP:''' (for the page that LUA expects to be at ''<nowiki>http://www.example.com/page.php</nowiki>'') | '''PHP:''' (for the page that LUA expects to be at ''<nowiki>http://www.example.com/page.php</nowiki>'') | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua" lang="php"> | ||
include( "mta_sdk.php" ); | include( "mta_sdk.php" ); | ||
$input = mta::getInput(); | $input = mta::getInput(); | ||
Line 64: | Line 64: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
'''LUA:''' | '''LUA:''' | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua" lang="lua"> | ||
-- result is called when the function returns | -- result is called when the function returns | ||
function result(sum) | function result(sum) |
Revision as of 18:03, 18 July 2009
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 "callFunction" 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.
Functions
call
This function calls an exported function in a specific resource.
Syntax:
$mtaServer = new mta( $hostname, $port, $username, $password ); $resource = $mtaServer->getResource ( $resourceName ); $returns[] = $resource->call ( "functionName" [,args...] )
getInput
This function is for use with web pages that are called by callRemote.
Syntax:
inputData[] = mta::getInput();
doReturn
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.
Syntax:
mta::doReturn( argument1, argument2 ... argumentN );
Examples
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 $resource = $retn[0]; // the first returned value is the resource $retn = $resource->call ( "getResourceName", $resource ); $resourceName = $retn[0]; // contains the name of the resource 'echobot'
Authentication
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.
Example:
include( "mta_sdk.php" ); $mtaServer = new mta("example.com", 33004, "myUsername", "myPassword" ); $mtaServer->getResource("someResource")->call("someFunction");
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]);
LUA:
-- result is called when the function returns function result(sum) outputChatBox(sum) end function addNumbers(number1, number2) callRemote ( "http://www.example.com/page.php", result, number1, number2 ) end addNumbers ( 123, 456 ) -- call the function
Caveats
- 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 either the "id" fields.
Download
- Download Version 0.3 - Neater syntax, support functions for callRemote (fix version makes call work with args).
- Download Version 0.2 - Deprecated - Syntax differs from examples shown above. - Adds authentication support
- Download Version 0.1 - Deprecated - Syntax differs from examples shown above.