将简单的Python2代码转换为PHP
抱歉再次打扰,但我真的需要帮助,把这段Python2的代码转换成PHP。
net, cid, lac = 25002, 9164, 4000
import urllib
a = '000E00000000000000000000000000001B0000000000000000000000030000'
b = hex(cid)[2:].zfill(8) + hex(lac)[2:].zfill(8)
c = hex(divmod(net,100)[1])[2:].zfill(8) + hex(divmod(net,100)[0])[2:].zfill(8)
string = (a + b + c + 'FFFFFFFF00000000').decode('hex')
data = urllib.urlopen('http://www.google.com/glm/mmap',string)
r = data.read().encode('hex')
print float(int(r[14:22],16))/1000000, float(int(r[22:30],16))/1000000
如果有人能帮忙,那就太好了,提前谢谢你们!
补充:
我明白了。你能编辑一下你的帖子,把你已经翻译的部分也放上来吗?
<?php
$net = 25002;
$cid = 9164;
$lac = 4000;
$a = '000E00000000000000000000000000001B0000000000000000000000030000'
$b = hex($cid)[2:].zfill(8) + hex($lac)[2:].zfill(8)
$c = hex(divmod($net,100)[1])[2:].zfill(8) + hex(divmod($net,100)[0])[2:].zfill(8)
$string = ($a + $b + $c + 'FFFFFFFF00000000').decode('hex')
$data = 'http://www.google.com/glm/mmap'.$string
$r = $data.read().encode('hex')
print float(int($r[14:22],16))/1000000, float(int($r[22:30],16))/1000000
?>
2 个回答
0
有一个小程序可以把Python代码转换成PHP代码。你可以试试这个转换工具,地址是 https://github.com/bunkahle/py2php。
5
因为在我午休的时候,魔兽世界的服务器宕机了:
// net, cid, lac = 25002, 9164, 4000
$net = 25002;
$cid = 9164;
$lac = 4000;
// import urllib
//a = '000E00000000000000000000000000001B0000000000000000000000030000'
$a = '000E00000000000000000000000000001B0000000000000000000000030000';
//b = hex(cid)[2:].zfill(8) + hex(lac)[2:].zfill(8)
$b = sprintf("%08x%08x", $cid, $lac);
//c = hex(divmod(net,100)[1])[2:].zfill(8) + hex(divmod(net,100)[0])[2:].zfill(8)
$c = sprintf("%08x%08x", $net % 100, floor($net / 100));
//string = (a + b + c + 'FFFFFFFF00000000').decode('hex')
$string = $a . $b . $c . 'FFFFFFFF00000000';
$newstring = '';
for( $i = 0, $count = strlen($string); $i < $count; $i++ ) {
$newstring .= sprintf("%c", hexdec($string{$i} . $string{++$i}));
}
//data = urllib.urlopen('http://www.google.com/glm/mmap',string)
$ch = curl_init('http://www.google.com/glm/mmap');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $newstring);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//r = data.read().encode('hex')
$r = curl_exec($ch);
//print float(int(r[14:22],16))/1000000, float(int(r[22:30],16))/1000000
$r = array_pop(unpack("H*", $r));
printf("%f, %f", hexdec(substr($r, 14, 8)) / 1000000, hexdec(substr($r, 22, 8)) / 1000000);
不过我还是希望能看到更优雅的十六进制转换方式。