|
| 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