2014-01-27 10:39 阅读 32

我已经尝试了每个方法来返回JSON编码的PHP对象 - 为什么它不起作用?


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:


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


    <pre class='xdebug-var-dump' dir='ltr'>
      <i>public</i> 'Password' <font color='#888a85'>=&gt;</font> <small>string</small><font color='#cc0000'>'derp'</font> <i>(length=4)</i>
      <i>public</i> 'Host' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'localhost'</font> <i>(length=9)</i>
      <i>public</i> 'Port' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'25575'</font> <i>(length=5)</i>
      <i>public</i> '_Sock' <font color='#888a85'>=&gt;</font> <b>resource</b>(<i>4</i><font color='#2e3436'>,</font> <i>stream</i>)
      <i>public</i> '_Id' <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>1</font>

This is essentially what I'm trying to do:

  1. Create an RCON connection to a Minecraft server by sending a $.post request to PHP, which will establish the connection.
  2. Return a response ("Authenticated" or "Authentication failed") to indicate success and the created object for later use (to avoid creating multiple connections.)
  3. 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.)

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dongzou3751 dongzou3751 2014-01-27 10:44

    As the documentation states, you cannot encode resource types with json_encode. The current code attempts to do this because the class minecraftRcon exposes the property $_Sock whose value is a resource.

    However, it's not clear exactly why your JS/PHP code is not working as a whole. Since you can't access (or even see) anything "inside" a resource value from PHP it's almost certain that you are not trying to do that from JS, so while the warning is valid and you should fix it, it probably does not bear at all on your current problem.

    点赞 评论 复制链接分享
  • dqeonr8554 dqeonr8554 2014-01-27 10:46

    You can't just encode an object directly in json_encode().

    You need to convert your object to an array, you could use something like:

    $data = get_object_vars($r)

    And encode $data

    点赞 评论 复制链接分享