doufen3786 2014-01-27 10:39
浏览 33
已采纳

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

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'>=&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>
    </pre>

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?