在路线中返回对象

时间:2014-06-07 05:56:56

标签: php laravel

我需要实现MinecraftQuery的类(https://github.com/xPaw/PHP-Minecraft-Query/blob/master/MinecraftQuery.class.php

到目前为止,我有PHP方面的工作。

这位于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 . "\n");
        }
    }

    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
});

但是这会返回错误。当我访问http://mysite.dev/servers?ip=lorem.someip.com&port=25564

我明白了......

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

但是,执行dd($ info)会返回...

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"
    // ...
  }
}

我从来没有见过一个数组:私有之前和我假设这是问题的一部分。但这是我想要回归的对象。

哦,最后一点,这就是我试图获取这些数据的方式..

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: \n" + data);
    })
    .fail(function(ex) {
        console.log(ex);
        return false;
    });

});

但是当然会返回500服务器错误,除非我在路由中执行dd()。

那么如何才能获得正确的数据?

2 个答案:

答案 0 :(得分:0)

查看课程,您似乎想要GetInfo()方法。这个例子应该有效:

$Query = new MinecraftQuery();
$host = $this->getHost($ip, $port);
$Query->Connect( $host["ip"], $host["port"] );
var_dump($Query->GetInfo());

答案 1 :(得分:0)

你可以试试这个:

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

done方法中尝试:

done(function(data) {
    var object = $.parseJSON(data);
    console.log(object);
})