dongshi9719
dongshi9719
2014-06-07 05:56

在路线中返回对象

  • laravel
  • php

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条回答