PHP SDK: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
Line 7: Line 7:


==Functions==
==Functions==
===callFunction===
===call===
This function calls an exported function in a specific resource.
'''Syntax:'''
'''Syntax:'''
<syntaxhighlight lang="lua">[php]
<syntaxhighlight lang="lua">[php]
$mtaSDK = new mta();
$mtaServer = new mta( $hostname, $port, $username, $password );
$returns[] = $mtaSDK->callFunction( $hostname, $port, "resourceName", "functionName" [,args...] );
$resource = $mtaServer->getResource ( $resourceName );
$returns[] = $resource->call ( "functionName" [,args...] )
</syntaxhighlight>
 
===getInput===
This function is for use with web pages that are called by [[callRemote]].
'''Syntax:'''
<syntaxhighlight lang="lua">[php]
inputData[] = mta::getInput();
</syntaxhighlight>
 
===doReturn===
Use this function when you want to return data when a page is called with [[callRemote]].
'''Syntax:'''
<syntaxhighlight lang="lua">[php]
mta::doReturn( argument1, argument2 ... argumentN );
</syntaxhighlight>
</syntaxhighlight>


Line 17: Line 33:
<syntaxhighlight lang="lua">[php]
<syntaxhighlight lang="lua">[php]
include( "mta_sdk.php" );
include( "mta_sdk.php" );
$mtaSDK = new mta();
$mtaServer = new mta("bastage.student.utwente.nl", 33004);
$retn = $mtaSDK->callFunction( "bastage.student.utwente.nl", 33004, "echobot", "getThisResource" ); // $retn is an array containing the values the function returned
$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
$resource = $retn[0]; // the first returned value is the resource
$retn = $mtaSDK->callFunction( "bastage.student.utwente.nl", 33004, "echobot", "getResourceName", $resource );  
$retn = $resource->( "getResourceName", $resource );  
$resourceName = $retn[0]; // contains the name of the resource 'echobot'
$resourceName = $retn[0]; // contains the name of the resource 'echobot'
</syntaxhighlight>
</syntaxhighlight>
Line 30: Line 47:
<syntaxhighlight lang="lua">[php]
<syntaxhighlight lang="lua">[php]
include( "mta_sdk.php" );
include( "mta_sdk.php" );
$mtaSDK = new mta();
$mtaServer = new mta(myServer", 33004, "myUsername", "myPassword" );
$mtaSDK->http_username = "myUsername";
$mtaServer->getResource("someResource")->call("someFunction");
$mtaSDK->http_password = "myPassword";
</syntaxhighlight>
$mtaSDK->callFunction( "myServer", 33004, "someResource", "someFunction" );
 
==A page that can be called by [[callRemote]]==
This example just adds two numbers passed to it by a lua script.
'''PHP:'''
<syntaxhighlight lang="lua">[php]
include( "mta_sdk.php" );
$input = mta::getInput();
mta::doReturn($input[0] + $input[1]);
</syntaxhighlight>
'''LUA:'''
<syntaxhighlight lang="lua">
-- result is called when the function returns
function result(sum)
    outputChatBox(sum)
end
function addNumbers(number1, number2)
    callRemote ( "http://chegworthvalley.dreamhosters.com/test.php", result, number1, number2 )
end
addNumbers ( 123, 456 ) -- call the function
</syntaxhighlight>
</syntaxhighlight>


==Caveats==
==Caveats==
* This only works with PHP 5.0 and above.
* This only works with PHP 5.0 and above.
* You cannot currently compare two Resource or Element instances that you expect to be identical - you need to do a "deep compare", comparing either the "id" fields or the "name" fields.
* 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==
*[http://misc.opencoding.net/mta/mtaphpsdk_0.2.zip Download Version 0.2] - Adds authentication support
*[http://misc.opencoding.net/mta/mtaphpsdk_0.2.zip Download Version 0.2] -  ''Deprecated - Syntax differs from examples shown above.'' - Adds authentication support
*[http://misc.opencoding.net/mta/mtaphpsdk_0.1.zip Download Version 0.1] - ''Deprecated - Syntax differs from examples shown above.''
*[http://misc.opencoding.net/mta/mtaphpsdk_0.1.zip Download Version 0.1] - ''Deprecated - Syntax differs from examples shown above.''

Revision as of 18:20, 18 November 2007

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:

[php]
$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:

[php]
inputData[] = mta::getInput();

doReturn

Use this function when you want to return data when a page is called with callRemote. Syntax:

[php]
mta::doReturn( argument1, argument2 ... argumentN );

Examples

[php]
include( "mta_sdk.php" );
$mtaServer = new mta("bastage.student.utwente.nl", 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->( "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:

[php]
include( "mta_sdk.php" );
$mtaServer = new mta(myServer", 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:

[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://chegworthvalley.dreamhosters.com/test.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