2

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

?>
6
  • Even though that's a dead link (error 400)? Commented 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. Commented 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. Commented Apr 13, 2010 at 15:19
  • 1
    If you would, please let us know specifically which lines of code you are having troubling converting to PHP. Commented 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') Commented Apr 13, 2010 at 15:34

2 Answers 2

5

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

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.
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');
uploaded it to a server ( backstreetcat.com/mobi/test.php ), doesn't give an error, just 0.000000, 0.000000
You must not have curl support in PHP. You'll have to get that sorted out (there are some related questions on SO) or find an alternative HTTP library.
What are mcc and mnc? The above script only deals with net, cid, and lac inputs, per your original question.
|
0

there is a little program to convert Python code to PHP: You can try the converter from https://github.com/bunkahle/py2php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.