Sorry to bother again, but I really need help transforming this Python2 code into 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
Would be great if someone could help, thanks in advance!
EDIT:
I see. Can you edit your post to include what you've translated so far please.
<?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
?>
Limon Monte
54.8k49 gold badges191 silver badges222 bronze badges
asked Apr 13, 2010 at 15:03
Roger Travis
8,57018 gold badges69 silver badges98 bronze badges
-
Even though that's a dead link (error 400)?Matt Luongo– Matt Luongo2010年04月13日 15:09:06 +00:00Commented Apr 13, 2010 at 15:09
-
Show us what you've done so far. Also be mindful that you rarely upvote or accept answers which makes many folks shy away from assisting.webbiedave– webbiedave2010年04月13日 15:09:28 +00:00Commented Apr 13, 2010 at 15:09
-
>>> Also be mindful that you rarely upvote or accept answers which makes many folks shy away from assisting. Sorry for that, still new to this site, already reviewed all my questions and marked answers as useful and accepted.Roger Travis– Roger Travis2010年04月13日 15:19:41 +00:00Commented Apr 13, 2010 at 15:19
-
1If you would, please let us know specifically which lines of code you are having troubling converting to PHP.webbiedave– webbiedave2010年04月13日 15:30:14 +00:00Commented Apr 13, 2010 at 15:30
-
basically I'm afraid I don't understand what that HEX stuff is. 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')Roger Travis– Roger Travis2010年04月13日 15:34:31 +00:00Commented Apr 13, 2010 at 15:34
2 Answers 2
Because the World of Warcraft servers are down during my lunch break:
// 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);
I would love to see more elegant hex conversion, though.
Sign up to request clarification or add additional context in comments.
16 Comments
Roger Travis
well, yes. this are the lat/long coordinates for the sample net/cid/lac given in the python script. it shows that the script works. what I need is to transform this script into php.
Roger Travis
Wow... thanks man. a... somehow it pops an error 'Fatal error: Call to undefined function curl_init() in C:\xampp\xampp\htdocs\mobi\test.php on line 41'. Line 41 is $ch = curl_init('google.com/glm/mmap');
Roger Travis
uploaded it to a server ( backstreetcat.com/mobi/test.php ), doesn't give an error, just 0.000000, 0.000000
|
there is a little program to convert Python code to PHP: You can try the converter from https://github.com/bunkahle/py2php
Comments
default