将CID转换为LAT时,PHP会产生不同的结果

时间:2013-07-22 05:19:20

标签: php google-maps

我有脚本将CID转换为LAT,LAC转换为LONG。在我的localhost中,一切都很好,但在生产服务器中(使用相同的脚本)给我不同的结果。

localhost中的结果: CID = 55073 => LAT = -6.254678(TRUE RESULT)

生成服务器的结果 CID = 55073 => LAT = 4288.712618(不正确的结果)

这是我的剧本

if(isset($lac) && isset ($cid)) 
    {

        $data = "\x00\x0e" .
        "\x00\x00\x00\x00\x00\x00\x00\x00" .
        "\x00\x00" .
        "\x00\x00" .
        "\x00\x00" .
        "\x1b" .
        "\x00\x00\x00\x00" .
        "\x00\x00\x00\x00" .
        "\x00\x00\x00\x00" .
        "\x00\x00" .
        "\x00\x00\x00\x00" .
        "\x00\x00\x00\x00" .
        "\x00\x00\x00\x00" .
        "\x00\x00\x00\x00" .
        "\xff\xff\xff\xff" .
        "\x00\x00\x00\x00";

        $is_umts_cell = ($cid > 65535);
        if ($is_umts_cell) // GSM: 4 hex digits, UTMS: 6 hex digits
            $data[0x1c] = 5;
        else
            $data[0x1c] = 3;

        $hexlac = substr("00000000" . dechex($lac), -8);
        $hexcid = substr("00000000" . dechex($cid), -8);

        $data[0x1f] = pack("H*", substr($hexcid, 0, 2));
        $data[0x20] = pack("H*", substr($hexcid, 2, 2));
        $data[0x21] = pack("H*", substr($hexcid, 4, 2));
        $data[0x22] = pack("H*", substr($hexcid, 6, 2));

        $data[0x23] = pack("H*", substr($hexlac, 0, 2));
        $data[0x24] = pack("H*", substr($hexlac, 2, 2));
        $data[0x25] = pack("H*", substr($hexlac, 4, 2));
        $data[0x26] = pack("H*", substr($hexlac, 6, 2));

        /* I used file_get_contents() at my laptop webserver, but it seems like the PHP version
         * at my hosting company is old and it is not supporting that.
         * For the hosting company, here we're using cURL.
         */
        $use_curl = true;
        if ($use_curl) {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, "http://www.google.com/glm/mmap");
            curl_setopt($ch, CURLOPT_HEADER, true);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HTTPHEADER, Array (
                "Content-type: application/binary"
            ));
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            curl_setopt($ch, CURLOPT_POST, 1);
            $response = curl_exec($ch);
            if (curl_errno($ch))
                return -1;

            $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
            $str = substr($response, $header_size);

            curl_close($ch);
        } else {
            $context = array (
                'http' => array (
                    'method' => 'POST',
                    'header' => "Content-type: application/binary\r\n" . "Content-Length: " . strlen($data
                ) . "\r\n",
                'content' => $data
            ));
            $xcontext = stream_context_create($context);
            $str = file_get_contents("http://www.google.com/glm/mmap", FALSE, $xcontext);
            echo 'use post'; exit;
        }

        $opcode1 = ((ord($str[0]) << 8)) | ord($str[1]);
        $opcode2 = ord($str[2]);

        if (($opcode1 != 0x0e) || ($opcode2 != 0x1b))
            return -2;

        $retcode = ((ord($str[3]) << 24) | (ord($str[4]) << 16) | (ord($str[5]) << 8) | (ord($str[6])));
        if ($retcode != 0)
            return -2;

        $lat = ((ord($str[7]) << 24) | (ord($str[8]) << 16) | (ord($str[9]) << 8) | (ord($str[10]))) / 1000000;
        $lon = ((ord($str[11]) << 24) | (ord($str[12]) << 16) | (ord($str[13]) << 8) | (ord($str[14]))) / 1000000;

        // exit script if cannot geocode cell e.g. not on google's database
        if ($lat == 0 and $lon == 0)
            return -3;

        $data = array(
           'cellid' => $cid,
           'lac' => $lac,
           'lat' => $lat,
           'lon' => $lon,
        );

        if($retr)
        {
            return array (
                "lat" => $lat,
                "lng" => $lon
            );
        }else{
            header('Cache-Control: no-cache, must-revalidate');
            header('Content-type: application/json');
            echo json_encode(array(
                                'lat' => $lat,
                                'lng' => $lon
                            ));
        }
    }   

0 个答案:

没有答案