Skip to content

Commit 9deb1cd

Browse files
committed
Resource Web Access & SDKs
1 parent 706f654 commit 9deb1cd

File tree

3 files changed

+585
-0
lines changed

3 files changed

+585
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import AutoStarlightPage from '@src/components/AutoStarlightPage.astro';
2+
import { getSeeAlsoLinksFromList } from '@src/utils/general';
3+
import SeeAlsoSection from '@src/components/SeeAlsoSection.astro';
4+
import { Code } from '@astrojs/starlight/components';
5+
6+
<AutoStarlightPage frontmatter={{
7+
template: 'doc',
8+
title: 'JavaScript SDK',
9+
tableOfContents: false
10+
}}>
11+
12+
The JavaScript SDK for accessing the MTA Web Interface is currently part of the `ajax` resource.
13+
As such it can only be used in HTML pages hosted on the MTA server as part of a resource.
14+
To use it, place the following in the `<head>` section of your page.
15+
```lua
16+
<* = call(getResourceFromName("ajax"),"start", getResourceName(getThisResource()) ) *>
17+
```
18+
19+
Doing this automatically gives scripts in your page access to all the functions you have exported from your resource, under the same names.
20+
For example, if you have exported a `getPlayerCount` function, you can just call this from javascript:
21+
```javascript
22+
getPlayerCount ( function ( playerCount ) {
23+
alert ( "There are " + playerCount + " players on the server!" );
24+
} );
25+
```
26+
27+
Notice that this uses an anonymous function as an argument. This function is called when the function returns data.
28+
This allows you to continue doing other things in your script while waiting for a response.
29+
The arguments for the anonymous function are the same as the returned values from the function you're calling.
30+
31+
You can also manually call a script function using:
32+
33+
```javascript
34+
callFunction ( "resourceName", "functionName", returnFunction, errorFunction [, arguments... ] );
35+
```
36+
37+
- **returnFunction** - This is the function that is called when the result of the call is returned. The arguments should be the same as the returned values from the function.
38+
- **errorFunction** - This function is called when an error occurs. There is a single string argument that specifies what the error is.
39+
40+
Example
41+
```javascript
42+
callFunction ( "testResource", "getPlayerCount", function ( playerCount ) {
43+
alert( "There are " + playerCount + " players on the server!" );
44+
},
45+
function ( error ) {
46+
alert ( "Error getting player count: " + error );
47+
});
48+
```
49+
50+
<SeeAlsoSection seeAlsoLinks={getSeeAlsoLinksFromList([
51+
'reference:Resource_Web_Access',
52+
'reference:call',
53+
'reference:CEF-tutorial',
54+
])} currentId='' />
55+
56+
</AutoStarlightPage>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import AutoStarlightPage from '@src/components/AutoStarlightPage.astro';
2+
import { getSeeAlsoLinksFromList } from '@src/utils/general';
3+
import SeeAlsoSection from '@src/components/SeeAlsoSection.astro';
4+
import { Code } from '@astrojs/starlight/components';
5+
6+
<AutoStarlightPage frontmatter={{
7+
template: 'doc',
8+
title: 'PHP SDK',
9+
tableOfContents: false
10+
}}>
11+
12+
You can access the MTA Web Interface from almost any programming language that can request web pages. PHP can do this very easily.
13+
14+
This SDK provides one function call that will allow you to call any exported script functions on any server that you have access to.
15+
16+
## Installation
17+
### Prerequisites
18+
This SDK requires PHP 7.1 or greater.
19+
20+
### HTTPlug client abstraction
21+
**If you don't follow this requirement before require the SDK, composer will throw you an error.**
22+
23+
As this SDK uses HTTPlug, you will have to require some libraries for get it working. See [HTTPlug for library users](http://docs.php-http.org/en/latest/httplug/users.html) for more info.
24+
25+
### Setup
26+
The only supported installation method is via [Composer](https://getcomposer.org/). Run the following command to require this SDK in your project:
27+
```bash
28+
composer require multitheftauto/mtasa-php-sdk
29+
```
30+
31+
## Examples
32+
33+
There are three ways to call an MTA server's exported functions, as shown in the following example:
34+
35+
```php
36+
<?php
37+
38+
require_once('vendor/autoload.php');
39+
40+
use MultiTheftAuto\Sdk\Mta;
41+
use MultiTheftAuto\Sdk\Model\Server;
42+
use MultiTheftAuto\Sdk\Model\Authentication;
43+
44+
$server = new Server('127.0.0.1', 22005);
45+
$auth = new Authentication('myUser', 'myPassword');
46+
$mta = new Mta($server, $auth);
47+
48+
$response = $mta->getResource('someResource')->call('callableFunction', $arg1, $arg2, $arg3, ...);
49+
// or
50+
$response = $mta->getResource('someResource')->call->callableFunction($arg1, $arg2, $arg3, ...);
51+
52+
var_dump($response);
53+
```
54+
55+
### A page that can be called by [callRemote](/reference/callRemote)
56+
This example just adds two numbers passed to it by a Lua script.
57+
58+
PHP: (for the page that Lua expects to be at http://www.example.com/page.php)
59+
```php
60+
<?php
61+
62+
require_once('vendor/autoload.php');
63+
64+
use MultiTheftAuto\Sdk\Mta;
65+
66+
$input = Mta::getInput();
67+
Mta::doReturn($input[0] + $input[1]);
68+
```
69+
70+
```lua
71+
-- result is called when the function returns
72+
function result(sum)
73+
outputChatBox(sum)
74+
end
75+
function addNumbers(number1, number2)
76+
callRemote ( "http://www.example.com/page.php", result, number1, number2 )
77+
end
78+
addNumbers ( 123, 456 ) -- call the function
79+
```
80+
81+
## Releases
82+
Visit the [releases page on GitHub](https://github.com/multitheftauto/mtasa-php-sdk/releases) to download the SDK.
83+
84+
<SeeAlsoSection seeAlsoLinks={getSeeAlsoLinksFromList([
85+
'reference:Resource_Web_Access',
86+
'reference:CEF-tutorial',
87+
])} currentId='' />
88+
89+
</AutoStarlightPage>

0 commit comments

Comments
 (0)