将简单的Python2代码转换为PHP

问题描述 投票:2回答:2

很抱歉再次打扰,但我真的需要帮助将这个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

?>
php python
2个回答
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);

不过,我很想看到更优雅的十六进制转换。


0
投票

有一个程序可以将Python代码转换为PHP:您可以尝试从https://github.com/bunkahle/py2php转换

© www.soinside.com 2019 - 2024. All rights reserved.