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条)

报告相同问题?

悬赏问题

  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥15 pyqt信号槽连接写法
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。