tags:

views:

177

answers:

1

Hi guys, 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

?>
+3  A: 

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.

Adam Backstrom
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.
I edited my jerky answer.
Adam Backstrom
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('http://www.google.com/glm/mmap');
uploaded it to a server ( http://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.
Adam Backstrom
What are mcc and mnc? The above script only deals with net, cid, and lac inputs, per your original question.
Adam Backstrom
>>> What are mcc and mnc?net = MCC + MNC, in this example MCC = 250 and MNC 02, therefor net is 25002
>>> You must not have curl support in PHP.I upload to a server, no error there. But no result showing, too. see here http://backstreetcat.com/mobi/test.php
Could I see your test.php? Try copying the file to test.phps or test.txt, if there's no sensitive info within.
Adam Backstrom
done, see here: http://backstreetcat.com/mobi/test.txt
Works for me on two different servers, copy/pasted from your test.txt. I would say enable error reporting (`error_reporting(E_ALL); ini_set('display_errors', 1);`) and start outputting variables in the file until you find one that looks broken.
Adam Backstrom
>>> I would say enable error reporting (error_reporting(E_ALL);ini_set('display_errors', 1);) and start outputting variables in the file until you find one that looks broken.I'm afraid I didn't really understand that. where should I enable error reporting? I can edit the php to make it echo out the variables and find the broken one that way!
Include `error_reporting(E_ALL);` and `ini_set('display_errors', 1);` in your PHP file to enable full, noisy error reporting.
Adam Backstrom
http://backstreetcat.com/mobi/test.phpdone. ... where do I see the errors?
Apparently there aren't any. (Or they're in a log file somewhere.) I saw some of your debug output in test.php, and it seems that there is nothing returned when you do a curl_exec(). I think everything is fine up to that point, but for some reason curl is giving no output. You'll want to debug that problem to move forward.
Adam Backstrom
>>> but for some reason curl is giving no output.Therefor you think its something wrong with the server setting? i'll try to get into that and THANKS AGAIN FOR YOUR INPUT. :)
got an answer from my hosting support. the problem was that my plan did not have outgoing connections. :)
fixed ";extension=php_curl.dll" on my local xampp and it works! thanks again man!
No sweat, glad you got it going.
Adam Backstrom