dongshi9719 2014-06-07 05:56
浏览 31
已采纳

在路线中返回对象

I need to implement MinecraftQuery's class (https://github.com/xPaw/PHP-Minecraft-Query/blob/master/MinecraftQuery.class.php)

So far I have the PHP side working.

This resides in HomeController.

public function mcQuery($ip, $port)
{
    $Query = new MinecraftQuery();

    try
    {
        $host = $this->getHost($ip, $port);
        $Query->Connect( $host["ip"], $host["port"] );

        return $Query;
    }
    catch( MinecraftQueryException $e )
    {
        return false;
    }
}

public function getHost($address, $port)
{
    $result = dns_get_record("_minecraft._tcp.$address", DNS_SRV);
    if ($result) {
        $priority = 0;
        $valid = 0;
        foreach ($result as $v) {
            $type = $v['type'];
            $pri = $v['pri'];
            $targetPort = $v['port'];
            $target = $v['target'];
            if ($type=="SRV") {
                if ($valid==0 || $pri <= $priority) {
                    $address = $target;
                    $port = $targetPort;
                    $priority = $pri;
                    $valid = 1;
                }
            }
        }
    } else {
        $address = gethostbyname($address.'.');
        if(filter_var($address, FILTER_VALIDATE_IP) != $address) {
            throw new \Exception("Not a valid ip address: " . $address . "
");
        }
    }

    return [ 'ip' => $address, 'port' => $port ];
}
}

This works - by itself anyways, but the next problem is actually returning the response to the route so I can use it via javascript

This is all I have...

Route::get('/servers', function(){
    $ip = Input::get('ip');
    $port = Input::get('port');

    $home = App::make('HomeController');
    $info = $home->mcQuery($ip, $port);
    return $info
});

This returns an error, though. When I visit http://mysite.dev/servers?ip=lorem.someip.com&port=25564

I get...

The Response content must be a string or object implementing __toString(), "object" given.

However, doing a dd($info) instead returns...

object(MinecraftQuery)#130 (3) {
  ["Socket":"MinecraftQuery":private]=>
  resource(48) of type (Unknown)
  ["Players":"MinecraftQuery":private]=>
  NULL
  ["Info":"MinecraftQuery":private]=>
  array(10) {
    ["HostName"]=>
    string(37) "Some Name!"
    ["GameType"]=>
    string(3) "SMP"
    ["Version"]=>
    string(5) "1.7.9"
    // ...
  }
}

I've never seen an array with :private before and Im assuming that is apart of the problem. But this is the object I am trying to return.

Oh, and for the last bit, this is how I'm trying to get this data..

serversObj.each(function(index, el) {
    // serverObj is like 4 divs
    var serverIp = $(this).data('ip');
    var serverPort = $(this).data('port');
    var serverStatus = $(this).find('.status');


    $.ajax({
        url: '/servers',
        type: 'GET',
        data: {ip: serverIp, port: serverPort},
    })
    .done(function(data) {
        console.log("success: 
" + data);
    })
    .fail(function(ex) {
        console.log(ex);
        return false;
    });

});

But of course returns a 500 server error unless I do dd() in the route instead.

So how can I get the correct data to return?

  • 写回答

2条回答 默认 最新

  • douna1941 2014-06-07 13:24
    关注

    You may try this:

    return Response::json($info->GetInfo());
    

    In the done method try:

    done(function(data) {
        var object = $.parseJSON(data);
        console.log(object);
    })
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置