简体   繁体   English

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

[英]I have tried every method to return JSON encoded PHP objects - why won't it work?

Solution: 解:

Essentially, there isn't one. 基本上,没有一个。 The $_Sock is a resource, and thus cannot be passed through json_encode(). $ _Sock是一个资源,因此不能通过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.) 不幸的是,连接是通过$ .post建立的,这意味着它不能跨多个页面保存(除非像Jon建议的那样使用应用程序服务器。)

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. 我将不得不依赖于每次重新建立连接的模型,以便通过RCON服务器发送命令。

Thank you to Ryan and Jon for their immense help! 感谢Ryan和Jon的巨大帮助!


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: 我尝试过dataType: 'json'$.parseJSON()Header("Content-type: application/json") ,但是当我尝试返回一个json_encode()数组或对象时,我遇到了这个错误:

    Warning: json_encode(): type is unsupported, encoded as null

Here is the JavaScript $.post: 这是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+"\n");
    });

...and here is the PHP that returns the data: ...这里是返回数据的PHP:

    $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. 我已经在这3个小时了。 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. 我尝试过gettype ,它确认数据确实是一个对象。 json_encode should accept it, yet it is 'unsupported.' json_encode应该接受它,但它“不受支持”。 Please help - I am losing my sanity. 请帮助 - 我失去了理智。


var_dump($r): 后续代码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. 通过向PHP发送$ .post请求来创建与Minecraft服务器的RCON连接,这将建立连接。
  2. Return a response ("Authenticated" or "Authentication failed") to indicate success and the created object for later use (to avoid creating multiple connections.) 返回响应(“Authenticated”或“Authentication failed”)以指示成功和创建的对象供以后使用(以避免创建多个连接。)
  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. 通过这个已经创建的RCON对象发送命令,方法是将它返回到JS $ .post并将其存储在页面上的变量中。

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.) 以上所有操作都将通过一个.php页面完成,输出会被警告并打印到textarea(有时根本不起作用)。

As the documentation states, you cannot encode resource types with json_encode . 正如文档所述,您无法使用json_encode对资源类型进行编码。 The current code attempts to do this because the class minecraftRcon exposes the property $_Sock whose value is a resource. 当前代码尝试这样做是因为类minecraftRcon公开属性$_Sock其值是资源。

However, it's not clear exactly why your JS/PHP code is not working as a whole. 但是,目前还不清楚为什么你的JS / PHP代码不能整体运行。 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. 由于您无法从PHP访问(或甚至查看)任何“内部”资源值,因此几乎可以肯定您并未尝试从JS执行此操作,因此虽然警告有效并且您应该修复它,但它可能不会对你目前的问题充满信心。

You can't just encode an object directly in json_encode() . 您不能直接在json_encode()编码对象。

You need to convert your object to an array, you could use something like: 您需要将对象转换为数组,您可以使用以下内容:

$data = get_object_vars($r)

And encode $data 并编码$data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM