Solution:
Essentially, there isn't one. The $_Sock is a resource, and thus cannot be passed through json_encode(). Unfortunately, the connection is being established via a $.post, which means that it cannot be held across multiple pages (unless going to the trouble of using an application server as Jon suggested.)
Establishing the connection each time is of no particular inconvenience; it is merely an annoyance that can be tolerated. I will have to rely on a model that reestablishes the connection each time in order to send commands over the RCON server.
Thank you to Ryan and Jon for their immense help!
Original Post:
I have tried dataType: 'json'
, $.parseJSON()
, and Header("Content-type: application/json")
, but when I try to return a json_encode()
array or object, I am met with this error:
Warning: json_encode(): type is unsupported, encoded as null
Here is the JavaScript $.post:
$.post("rcon.php",
{
ip:server.ip,
port:server.rcon.port,
pwd:server.rcon.pwd
},
function(data){
alert(data);
$("#output").val($("#output").val()+data+"
");
});
...and here is the PHP that returns the data:
$r = new minecraftRcon($rconServer, $rconPort, $rconPass);
if ($r->Auth()) { $response = "Authenticated."; } else { $response = "Authentication failed."; }
echo json_encode($r);
I have been at this for 3 hours. I simply do not understand what I need to do to get this to work. I have tried gettype
, and it affirms that the data is indeed an object. json_encode
should accept it, yet it is 'unsupported.' Please help - I am losing my sanity.
var_dump($r):
<pre class='xdebug-var-dump' dir='ltr'>
<b>object</b>(<i>minecraftRcon</i>)[<i>1</i>]
<i>public</i> 'Password' <font color='#888a85'>=></font> <small>string</small><font color='#cc0000'>'derp'</font> <i>(length=4)</i>
<i>public</i> 'Host' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'localhost'</font> <i>(length=9)</i>
<i>public</i> 'Port' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'25575'</font> <i>(length=5)</i>
<i>public</i> '_Sock' <font color='#888a85'>=></font> <b>resource</b>(<i>4</i><font color='#2e3436'>,</font> <i>stream</i>)
<i>public</i> '_Id' <font color='#888a85'>=></font> <small>int</small> <font color='#4e9a06'>1</font>
</pre>
This is essentially what I'm trying to do:
- Create an RCON connection to a Minecraft server by sending a $.post request to PHP, which will establish the connection.
- Return a response ("Authenticated" or "Authentication failed") to indicate success and the created object for later use (to avoid creating multiple connections.)
- Send commands through this already-created RCON object by returning it to the JS $.post and storing it in a variable on the page.
All of the above will be done via one .php page and the output is alerted and printed to a textarea (which occasionally doesn't work at all.)