'LinKin Posted November 30, 2014 Share Posted November 30, 2014 Hello, I want to call a Lua function from my website, so using PHP SKD, I wrote this code inside a test2.php file in my website: <?PHP include("sdk/mta_sdk.php"); #$mtaServer = new mta("190.13.42.65", 22003); $mtaServer = new mta("190.168.1.4", 22003); $resource = $mtaServer->getResource ( "test" ); $retn = $resource->call ( "exportedFunction", "LINKIN" ); // $retn is an array containing the values the function returned ?> As you see I have 2 IP's. The first one is the IP address than is recognized by a webserver (I've another php file that prints the IP of the server). And the second one is the IP that appears in the server browser. The 22003 is the server's port (Not the HTTP port). I also tried with the server's HTTP port which is 22005, but it didn't work either. This is the message that appears in the internet browser when executing 'test2.php' file. Black rectangles are covering the path to the folder(s). This is the server-sided Lua script: function exportedFunction(arg) outputChatBox("ARG;: "..arg) end And this is the meta.xml <meta> <info name="Test" author="LinKin" type="script" version="1.0" description="PHP SDK Test" /> <min_mta_version client="1.3.4-0.00000" server="1.3.4-0.00000" /> <script src="server.lua" type="server" /> <export function="exportedFunction" type="server" http="true"/> </meta> I really don't know what to do here. Link to comment
MTA Team 0xCiBeR Posted November 30, 2014 MTA Team Share Posted November 30, 2014 It's in HTTP port you need to make the call. Make sure you can use the port on the webserver. <?php include("sdk/mta_sdk.php"); $user = "ENTERUSER"; $pass = "ENTERPASS"; $resource = "test"; $callFunction = "exportedFunction"; $message = "LINKIN"; $mtaServer = new mta("190.168.1.4", 22005,$user,$pass); $mtaServer->getResource($resource)->call($callFunction,$message); ?> Link to comment
'LinKin Posted November 30, 2014 Author Share Posted November 30, 2014 Hmm but what should I put as user and pass? Is that nessesary?, Like mandatory? Link to comment
novo Posted November 30, 2014 Share Posted November 30, 2014 You should specify an account with access to the "general.http" right (https://wiki.multitheftauto.com/wiki/Access_Control_List#HTTP_Interface), though I'm not sure you'll be able to call any function without giving any other rights to this account. And yes, it's necessary because you're actually going through the server's HTTP service with the API. Link to comment
'LinKin Posted December 1, 2014 Author Share Posted December 1, 2014 Does someone have a working example? I still can't make it, it keeps throwing the same error. Link to comment
MTA Team 0xCiBeR Posted December 1, 2014 MTA Team Share Posted December 1, 2014 Make sure you can use the port on the webserver. And make sure you are setting everything as it should be. The example i gave you works, i tested it myself. Link to comment
'LinKin Posted December 2, 2014 Author Share Posted December 2, 2014 This is the code: <?php include("sdk/mta_sdk.php"); $user = "LinKin"; $pass = "mypasshere"; $resource = "chattest"; $callFunction = "outputRemote"; $message = "LINKIN"; $mtaServer = new mta("23.235.234.83", 22020,$user,$pass); $mtaServer->getResource($resource)->call($callFunction,$message); ?> After trying some times, it showed another error: But it showed it just once, now it keeps showing the 'Could not connect' error.. What do you mean by making sure I can use the port on the webserver? And how everything should be set? Link to comment
qaisjp Posted December 2, 2014 Share Posted December 2, 2014 that's a php error you have there mate, it seems that your server settings (php server) are stopping you from including mta_sdk.php http://lmgtfy.co/?q=open_basedir+restriction+in+effect Link to comment
Feche1320 Posted December 3, 2014 Share Posted December 3, 2014 Looks like your host doesn't like include function, is it a free host? Try this: <?php/** ************************************ * MTA PHP SDK ************************************ * * @copyright Copyright © 2010, Multi Theft Auto * @author JackC, eAi, Sebas * @link http://www.mtasa.com * @version 0.4 */ class mta { private $useCurl = false; private $sockTimeout = 6; // seconds public $http_username = ''; public $http_password = ''; public $host = ''; public $port = ''; private $resources = array(); public function __construct( $host, $port, $username = "", $pass = "" ) { $this->host = $host; $this->port = $port; $this->http_username = $username; $this->http_password = $pass; } public function getResource ( $resourceName ) { foreach ( $this->resources as $resource ) { if ( $resource->getName == $resourceName ) return $resource; } $res = new Resource ( $resourceName, $this ); $this->resources[] = $res; return $res; } public static function getInput() { $out = mta::convertToObjects( json_decode( file_get_contents('php://input'), true ) ); return (is_array($out)) ? $out : false; } public static function doReturn() { $val = array(); for ( $i = 0; $i < func_num_args(); $i++ ) { $val[$i] = func_get_arg($i); } $val = mta::convertFromObjects($val); $json_output = json_encode($val); echo $json_output; } public function callFunction( $resourceName, $function, $args ) { if ( $args != null ) { $args = mta::convertFromObjects($args); $json_output = json_encode($args); } else { $json_output = ""; } $path = "/" . $resourceName . "/call/" . $function; $result = $this->do_post_request( $this->host, $this->port, $path, $json_output ); echo $json_output; $out = mta::convertToObjects( json_decode( $result, true ) ); return (is_array($out)) ? $out : false; } public static function convertToObjects( $item ) { if ( is_array($item) ) { foreach ( $item as &$value ) { $value = mta::convertToObjects( $value ); } } else if ( is_string($item) ) { if ( substr( $item, 0, 3 ) == "^E^" ) { $item = new Element( substr( $item, 3 ) ); } elseif ( substr( $item, 0, 3 ) == "^R^" ) { $item = $this->getResource( substr( $item, 3 ) ); } } return $item; } public static function convertFromObjects( $item ) { if ( is_array($item) ) { foreach ( $item as &$value ) { $value = mta::convertFromObjects($value); } } elseif ( is_object($item) ) { if ( get_class($item) == "Element" || get_class($item) == "Resource" ) { $item = $item->toString(); } } return $item; } function do_post_request( $host, $port, $path, $json_data ) { if ( $this->useCurl ) { $ch = curl_init(); curl_setopt( $ch, CURLOPT_URL, "http://{$host}:{$port}{$path}" ); curl_setopt( $ch, CURLOPT_POST, 1 ); curl_setopt( $ch, CURLOPT_POSTFIELDS, $json_data ); $result = curl_exec($ch); curl_close($ch); return $result; } else { if ( !$fp = @fsockopen( $host, $port, $errno, $errstr, $this->sockTimeout ) ) { throw new Exception( "Could not connect to {$host}:{$port}" ); } $out = "POST {$path} HTTP/1.0\r\n"; $out .= "Host: {$host}:{$port}\r\n"; if ( $this->http_username && $this->http_password ) { $out .= "Authorization: Basic " . base64_encode( "{$this->http_username}:{$this->http_password}" ) . "\r\n"; } $out .= "Content-Length: " . strlen($json_data) . "\r\n"; $out .= "Content-Type: application/x-www-form-urlencoded\r\n\r\n"; //$out .= "Connection: close\r\n\r\n"; $out .= $json_data . "\r\n\r\n"; if ( !fputs( $fp, $out ) ) { throw new Exception( "Unable to send request to {$host}:{$port}" ); } @stream_set_timeout( $fp, $this->sockTimeout ); $status = @socket_get_status($fp); $response = ''; while ( !feof($fp) && !$status['timed_out'] ) { $response .= fgets( $fp, 128 ); $status = socket_get_status($fp); } fclose( $fp ); $tmp = explode( "\r\n\r\n", $response, 2 ); $headers = $tmp[0]; $response = trim($tmp[1]); preg_match( "/HTTP\/1.(?:0|1)\s*([0-9]{3})/", $headers, $matches ); $statusCode = intval($matches[1]); if ( $statusCode != 200 ) { switch( $statusCode ) { case 401: throw new Exception( "Access Denied. This server requires authentication. Please ensure that a valid username and password combination is provided." ); break; case 404: throw new Exception( "There was a problem with the request. Ensure that the resource exists and that the name is spelled correctly." ); break; } } if ( preg_match( "/^error/i", $response ) ) { throw new Exception( ucwords( preg_replace("/^error:?\s*/i", "", $response ) ) ); } return $response; } } } class Element { var $id; function Element($id) { $this->id = $id; } function toString() { return "^E^" . $this->id; } } class Resource { var $name; private $server; function Resource($name, $server) { $this->name = $name; $this->server = $server; } function toString() { return "^R^" . $this->name; } public function getName() { return $this->name; } function call ( $function ) { $val = array(); for ( $i = 1; $i < func_num_args(); $i++ ) { $val[$i-1] = func_get_arg($i); } return $this->server->callFunction ( $this->name, $function, $val ); } } $user = "LinKin"; $pass = "mypasshere"; $resource = "chattest"; $callFunction = "outputRemote"; $message = "LINKIN"; $mtaServer = new mta("23.235.234.83", 22020,$user,$pass); $mtaServer->getResource($resource)->call($callFunction,$message); ?> Link to comment
'LinKin Posted December 3, 2014 Author Share Posted December 3, 2014 Well yes, it's a free host. I've tested it in a paid one, and it worked correctly. I've not tested your code tho, I didn't see it earlier when I readed your post. I'd like to ask something a little bit off-topic. - How can I detect a color code (#FF9911 for example) with php? - How can I break a string and turn it into 2 sub-strings? I'd research it myself but my time on the internet is quite restricted.. Thanks for your colaboration! Link to comment
MTA Team 0xCiBeR Posted December 4, 2014 MTA Team Share Posted December 4, 2014 1.) http://bavotasan.com/2011/convert-hex-color-to-rgb-using-php/ 2.) You should use explode function. Example: $str = "hi|how|are|you"; $delimiter = "|"; print_r(explode($delimiter,$str,4)); /*output: Array ( [0] => hi [1] => how [2] => are [3] => you )*/ Remember explode returns an array Link to comment
'LinKin Posted December 4, 2014 Author Share Posted December 4, 2014 Thank you CiBeR. @Feche1320: I've tried with your code (you just added the connection at the end of the script for not using the include function, right?) and it didn't work either. It keeps telling me that it could not connect... But I'm wondering.. Why does the PHP SDK works when communicating from Server to the Website? I can perfectly use callRemote to a .php page, do some code there, and return something else to the server. Link to comment
Feche1320 Posted December 4, 2014 Share Posted December 4, 2014 Thank you CiBeR.@Feche1320: I've tried with your code (you just added the connection at the end of the script for not using the include function, right?) and it didn't work either. It keeps telling me that it could not connect... But I'm wondering.. Why does the PHP SDK works when communicating from Server to the Website? I can perfectly use callRemote to a .php page, do some code there, and return something else to the server. Maybe the free host is blocking some ports Link to comment
Joker_Mta Posted December 4, 2014 Share Posted December 4, 2014 Thank you CiBeR.@Feche1320: I've tried with your code (you just added the connection at the end of the script for not using the include function, right?) and it didn't work either. It keeps telling me that it could not connect... But I'm wondering.. Why does the PHP SDK works when communicating from Server to the Website? I can perfectly use callRemote to a .php page, do some code there, and return something else to the server. Maybe the free host is blocking some ports +1 ,, try this : you should to add username and passowrd of your account in server , $ip = "ip"; $port = port; $user = "user"; $pass = "pass"; $resname = "resourceName"; require_once("sdk/mta_sdk.php"); $mtaServer = new mta($ip,$port,$user,$pass); $resource = $mtaServer->getResource ($resname); $table = $resource->call("online"); function online() return getPlayerCount() end meta: function="online" type="server" http="true"/> + add script to admin group Link to comment
MTA Team 0xCiBeR Posted December 5, 2014 MTA Team Share Posted December 5, 2014 I can perfectly use callRemote to a .php page, do some code there, and return something else to the server. If I'm not mistaken,callRemote uses port 80 to communicate with the web server. Hence the call works. Remember MTA SDK uses CURL. A workaround would be to just make calls whenever you need them. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now