XFawkes Posted April 23, 2013 Share Posted April 23, 2013 (edited) NVM the code works, the problem is that my mta server hoster allows only 22027 HTTP port which is closed / blocked by firewall on the web server. Any possibilites how to send the data from 1 to 2 servers? Hello, I am trying to create a server monitor on my website. The lua code looks like this: resource: WebSend function sendInfoHTTP() Players= {} for i,thePlayer in ipairs(getElementsByType("player")) do Players[i] = {} Players[i][1] = (getPlayerName(thePlayer) and getPlayerName(thePlayer) or "?") Players[i][2] = (getElementData(thePlayer,"name") and getElementData(thePlayer,"name") or "?") Players[i][3] = (getElementData(thePlayer,"surname") and getElementData(thePlayer,"surname") or "?") Players[i][4] = (getElementData(thePlayer,"id") and getElementData(thePlayer,"id") or "?") Players[i][5] = (getElementData(thePlayer,"pts") and getElementData(thePlayer,"pts") or "?") Players[i][6] = (getPlayerPing(thePlayer) and getPlayerPing(thePlayer) or "?") end return getPlayerCount(), getMaxPlayers (), Players end It works, the function is exported and when I am trying to connect from my browser: IP:Port/WebSend/call/sendInfoHTTP I get the correct results. [ 1, 60, [ [ "player1", "somename", "surname", 221, 4, 31 ] ,[ "player2", "somename2", "surname2", 11, 32, 64 ] ] ] The problem is that when I am trying to do it on my webside I can't connect to the game server [which stands on a different hoster]. My PHP code: <?php include( "mta_sdk.php" ); $host = 'xx.xx.xx.xx'; $port = 'xxxxx'; $user = 'user'; $pass = 'password'; $mtaServer = new mta($host, $port, $user, $pass); $resource = $mtaServer->getResource ( "WebSend" ); $retn = $resource->call ( "sendInfoHTTP" ); $pCount = $retn[0]; $pMax = $retn[1]; $pData = $retn[2]; ?> .... html code with the output, css styles, tables etc I get an error message: Fatal error: Uncaught exception 'Exception' with message 'Could not connect to xxxxxxxxxx' in xxxxxxx/mta_sdk.php:146 Stack trace: #0xxxxxxx/mta_sdk.php(79): mta->do_post_request('xxxxxx', '/WebSend/call/s...', '') #1 xxxxxxx/mta_sdk.php(257): mta->callFunction('WebSend', 'sendInfoHTTP', Array) #2 xxxxxxx/pages.php(167) : eval()'d code(10): Resource->call('sendInfoHTTP') #3 xxxxxxx/pages.php(167): eval() #4 xxxxxxx/pages.php(761): pageBuilder->recacheP in xxxxxxx/mta_sdk.php on line 146 the mta_sdk.php file I am using: <?php /** ************************************ * MTA PHP SDK ************************************ * * @copyright Copyright (C) 2010, Multi Theft Auto * @author JackC, eAi, Sebas * @link [url=http://www.mtasa.com]http://www.mtasa.com[/url] * @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 ) { Edited April 25, 2013 by Guest Link to comment
Jaysds1 Posted April 25, 2013 Share Posted April 25, 2013 Does your server have a static ip? If it doesn't then ever time you restart your hosting computer, then you would need to change the "host" variable. Link to comment
Dev Posted April 25, 2013 Share Posted April 25, 2013 Eh, I need to ask something, how are you getting the results on your browser without using the php echo function? I'm trying to do the same thing, get a return from a .lua file with http as true in the meta export, but nothing seems to be returned to the .php page. Link to comment
XFawkes Posted April 25, 2013 Author Share Posted April 25, 2013 NVM the code works, the problem is that my mta server hoster allows only 22027 HTTP port which is closed / blocked by firewall on the web server. Any possibilites how to send the data from 1 to 2 servers? 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