Skip to content

Instantly share code, notes, and snippets.

@unwiredlabs
Last active September 14, 2022 05:18
Show Gist options
  • Save unwiredlabs/6081175 to your computer and use it in GitHub Desktop.
Save unwiredlabs/6081175 to your computer and use it in GitHub Desktop.
This script is a demo on how to use LocationAPI.org's Mobile Triangulation API v2.
<?php
/*
* Sample script to test Unwired Labs Geolocation API v2
*
* Created: 20th July, 2013
* Author: Unwired Labs
*
*/
// The data to send to the API
$postData = Array
(
"token" => "your_token",
"radio"=>"gsm",
"mcc" => "310",
"mnc" => "410",
"cells" => Array
(
"0" => Array
(
"lac" => "7033",
"cid" => "17811",
),
"1" => Array
(
"lac" => "7033",
"cid" => "17812",
),
"2" => Array
(
"lac" => "7033",
"cid" => "18513",
)
)
);
// Setup cURL
$ch = curl_init('http://us1.unwiredlabs.com/v2/process.php');
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
CURLOPT_POSTFIELDS => json_encode($postData)
));
// Send the request
$response = curl_exec($ch);
// Check for errors
if ($response === FALSE) {
die(curl_error($ch));
}
print_r($response);
?>
@dangra
Copy link

dangra commented Sep 26, 2013

Hello @LocationAPI, I'd like to contribute a little script in python to query your wonderful service

example usage:

$ ./locationapi.py --token XXXX --mcc 748 --mnc 7 66862/9020
{'token': 'XXXX', 'mnc': 7, 'mcc': 748, 'cells': [{'lac': 9020, 'cid': 66862}]}
{u'status': u'ok', u'lat': -34.64405, u'balance': 38, u'lon': -54.16642}
https://maps.google.com/?q=-34.64405,-54.16642

locationapi.py

#!/usr/bin/env python
import sys
from argparse import ArgumentParser
from json import dumps
import requests


def _cid_slash_lac(txt):
    cid, lac = txt.split('/')
    return int(cid), int(lac)


def main():
    ap = ArgumentParser()
    ap.add_argument('--token', default='CHANGEME')
    ap.add_argument('--endpoint', default='http://locationapi.org/v2/process.php')
    ap.add_argument('--mcc', type=int, default=748)  # Uruguay
    ap.add_argument('--mnc', type=int, default=7)  # Movistar
    ap.add_argument('cells', nargs='+', type=_cid_slash_lac)
    args = ap.parse_args()
    # locationapi accepts up to 5 cells per call
    assert len(args.cells) <= 5

    payload = {
        'token': args.token,
        'mcc': args.mcc,
        'mnc': args.mnc,
        'cells': [{'cid': cid, 'lac': lac} for cid, lac in args.cells],
    }
    print payload
    rsp = requests.post(url=args.endpoint,
                        data=dumps(payload),
                        headers={'content-encoding': 'application/json'})

    o = rsp.json()
    print o
    if o.get('status') == 'ok':
        print 'https://maps.google.com/?q={lat},{lon}'.format(**o)


if __name__ == '__main__':
    sys.exit(main())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment