PHP SDK: Difference between revisions
Jump to navigation
Jump to search
(Updated details for version 0.2 of the SDK and add information on authentication) |
|||
Line 10: | Line 10: | ||
'''Syntax:''' | '''Syntax:''' | ||
<syntaxhighlight lang="lua">[php] | <syntaxhighlight lang="lua">[php] | ||
returns[] = callFunction ( | $mtaSDK = new mta(); | ||
$returns[] = $mtaSDK->callFunction( $hostname, $port, "resourceName", "functionName" [,args...]); | |||
</syntaxhighlight> | </syntaxhighlight> | ||
==Examples== | ==Examples== | ||
<syntaxhighlight lang="lua">[php] | <syntaxhighlight lang="lua">[php] | ||
include ( " | include( "mta_sdk.php" ); | ||
$retn = callFunction ( "bastage.student.utwente.nl | $mtaSDK = new mta(); | ||
$retn = $mtaSDK->callFunction( "bastage.student.utwente.nl", 33004, "echobot", "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 = callFunction ( "bastage.student.utwente.nl | $retn = $mtaSDK->callFunction( "bastage.student.utwente.nl", 33004, "echobot", "getResourceName", $resource ); | ||
$resourceName = $retn[0]; // contains the name of the resource 'echobot' | $resourceName = $retn[0]; // contains the name of the resource 'echobot' | ||
</syntaxhighlight> | |||
==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:''' | |||
<syntaxhighlight lang="lua">[php] | |||
include( "mta_sdk.php" ); | |||
$mtaSDK = new mta(); | |||
$mtaSDK->http_username = "myUsername"; | |||
$mtaSDK->http_password = "myPassword"; | |||
$mtaSDK->callFunction( "myServer", 33004, "someResource", "someFunction" ); | |||
</syntaxhighlight> | </syntaxhighlight> | ||
==Caveats== | ==Caveats== | ||
* This only | * 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 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. | ||
==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] - Adds authentication support | ||
*[http://misc.opencoding.net/mta/mtaphpsdk_0.1.zip Download Version 0.1] | *[http://misc.opencoding.net/mta/mtaphpsdk_0.1.zip Download Version 0.1] - ''Deprecated - Syntax differs from examples shown above.'' |
Revision as of 00:09, 11 July 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
callFunction
Syntax:
[php] $mtaSDK = new mta(); $returns[] = $mtaSDK->callFunction( $hostname, $port, "resourceName", "functionName" [,args...]);
Examples
[php] include( "mta_sdk.php" ); $mtaSDK = new mta(); $retn = $mtaSDK->callFunction( "bastage.student.utwente.nl", 33004, "echobot", "getThisResource" ); // $retn is an array containing the values the function returned $resource = $retn[0]; // the first returned value is the resource $retn = $mtaSDK->callFunction( "bastage.student.utwente.nl", 33004, "echobot", "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" ); $mtaSDK = new mta(); $mtaSDK->http_username = "myUsername"; $mtaSDK->http_password = "myPassword"; $mtaSDK->callFunction( "myServer", 33004, "someResource", "someFunction" );
Caveats
- 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.
Download
- Download Version 0.2 - Adds authentication support
- Download Version 0.1 - Deprecated - Syntax differs from examples shown above.