这是我用于获取游戏服务器ip并返回名称的功能。我所在的网络主机不支持这种方法,所以我希望将它完全转换为curl来尝试。有什么想法吗?
$ip = $serverip;
queryport = 29416;
$socket = @fsockopen("udp://".$ip, $queryport , $errno, $errstr, 1);
stream_set_timeout($socket, 1);
stream_set_blocking($socket, TRUE);
fwrite($socket, "\xFF\xFF\xFF\xFF\x54Source Engine Query\x00");
$response = fread($socket, 4096);
@fclose($socket);
$packet = explode("\x00", substr($response, 6), 5);
$server = array();
$server['name'] = $packet[0];
$servername = ( $server['name'] );
提前致谢。
答案 0 :(得分:0)
最初你将$ serverip分配给$ ip;检查您是否获得了正确的价值。否则,您可以使用下面的卷曲代码来获取IP地址。
$url = 'http://gmail.com';
$wrapper = fopen('php://temp', 'r+');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, $wrapper);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
$ips = get_curl_ips($wrapper);
fclose($wrapper);
echo end($ips);
function get_curl_ips($fp){ rewind($fp);
$str = fread($fp, 8192);
$regex = '/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/';
if (preg_match_all($regex, $str, $matches)) { return array_unique($matches[0]); }
else { return false; } }