Common PHP interface for wifi positioning services, inspired by whoami.
$ composer require tuupola/whereami $ composer require php-http/curl-client
If you do not install HTTP client you will get
No HTTPlug clients found and
Puli Factory is not available errors. Ignore the Puli part, you do not need it. Just install an HTTP client.
Finally if your project does not already have one, you must include a PSR-7 implementation. Good candidate is
zendframework/zend-diactoros. Many frameworks such as Slim and Expressive already include PSR-7 by default and this part is not needed.
$ composer require zendframework/zend-diactoros
To find current computers location you must provide wifi location service provider and optionally a wifi network scanner. With macOS you can use
AirportScanner which does not require any extra setup. With Linux based systems you can use
IwlistScanner which needs root privileges to be run. Example below uses Mozilla Location Service.
require __DIR__ . "/vendor/autoload.php"; use Whereami\Provider\MozillaProvider; use Whereami\Scanner\AirportScanner; use Whereami\Whereami; $provider = new MozillaProvider("your-api-key-here"); $scanner = new AirportScanner; $locator = new Whereami($provider, $scanner); $location = $locator->whereami(); /* Array ( [latitude] => 1.355989 [longitude] => 103.992365 [accuracy] => 65 ) */
Pro tip! Like mentioned above providing scanner is optional. If scanner is not provided system will try to autodetect it. This way same code should work in both macOS and *NIX systems.
require __DIR__ . "/vendor/autoload.php"; use Whereami\Provider\MozillaProvider; use Whereami\Whereami; $provider = new MozillaProvider("your-api-key-here"); $locator = new Whereami($provider); $location = $locator->whereami();
Third party location
Sometimes it is useful to locate third party locations. For example you might have several IoT devices whose location you need to track. You can locate these by calling
$locator->whereis($networks) method with network info as a parameter.
require __DIR__ . "/vendor/autoload.php"; use Whereami\Provider\MozillaProvider; use Whereami\Whereami; $provider = new MozillaProvider("your-api-key-here"); $locator = new Whereami($provider); $networks = [ "name" => "#WiFi@Changi", "address" => "64:d8:14:72:60:0c", "signal" => -90, "channel" => 149, ]; $networks = [ "name" => "#WiFi@Changi", "address" => "10:bd:18:5f:e9:83", "signal" => -70, "channel" => 6, ]; $location = $locator->whereis($networks); /* Array ( [latitude] => 1.3558172 [longitude] => 103.9915859 [accuracy] => 38 ) */
use Whereami\Provider\CombainProvider; $provider = new CombainProvider("your-api-key-here");
Google Geolocation Provider
use Whereami\Provider\GoogleProvider; $provider = new GoogleProvider("your-api-key-here");
Google Browserlocation Provider
This provider uses old undocumented browserlocation API. It does not require API key.
use Whereami\Provider\BrowserlocationProvider; $provider = new BrowserlocationProvider(null);
This provider uses Mozilla Location Service (MLS). It requires an API key but you can use the key
test for developing.
use Whereami\Provider\MozillaProvider; $provider = new MozillaProvider("your-api-key-here");
This provider uses Radiocells Network Geolocation Services API. It is free to use and does not require an API key.
use Whereami\Provider\RadiocellsProvider; $provider = new RadiocellsProvider(null);
use Whereami\Provider\UnwiredProvider; $provider = new UnwiredProvider("your-api-key-here");
Scanners scan the surrounding wifi networks and return the results as an array. This array is then given to a provider which uses the network data to retrieve the geoposition.
This is the default scanner for macOS. It uses the
airport commandline tool from Apple80211 framework. Custom command can be passed via constructor. This is optional and should be used for example if your
airport binary is located in non standard place
use Whereami\Scanner\AirportScanner; $scanner = new AirportScanner("/tmp/airport --scan 2>&1");
Iwlist With Sudo
This is the default scanner for Linux and other *NIX based systems. It uses the iwlist commandline tool. This scanner is bit more complicated to set up since it needs root privileges to be run.
There are several ways to get around this. The default one is to give your webserver privileges to run
sudo iwlist without password by editing the
$ sudo visudo
Then add the following to the end of the file.
apache ALL=(ALL) NOPASSWD: /sbin/iwlist
After editing the file save and exit. Make sure you can run the command as your webserver user.
$ sudo su apache $ /sbin/iwlist scan
If you can see the scan results you are good to go.
use Whereami\Scanner\IwlistScanner; $scanner = new IwlistScanner;
Iwlist Without Sudo
If you do not want to give the webserver privileges to run
iwlist as root you could set up a root cronjob which writes the scan results to text file instead.
$ sudo crontab -e
Add the following line to the crontab. This will run the scan every 10 minutes.
*/10 * * * * /sbin/iwlist scan > /tmp/iwlist.txt
After editing the file save and exit. Wait for 10 minutes to make sure your cron entry works.
$ cat /tmp/iwlist.txt
If you can see the scan results you are good to go. However you must use custom command which uses the output from the cronjob.
use Whereami\Scanner\IwlistScanner; $scanner = new IwlistScanner("cat /tmp/iwlist.txt");
Some operating have commandline tools for determining the current position. Adapters provide an interface for these. You can use an adapter for developing if you do not have access to any external geopositioning provider.
require __DIR__ . "/vendor/autoload.php"; use Whereami\Adapter\CoreLocationAdapter; use Whereami\Whereami; $adapter = new CoreLocationAdapter; $locator = new Whereami($adapter); $location = $locator->whereami();
Note that adapters always return the location of the current machine. They cannot be used to determine third party location. Trying to do so will trigger a PHP warning.
$ brew install corelocationcli
use Whereami\Adapter\CoreLocationAdapter; $adapter = new CoreLocationAdapter;
$ brew install locateme
use Whereami\Adapter\LocateMeAdapter; $adapter = new LocateMeAdapter;
You can run tests either manually or automatically on every code change. Automatic tests require entr to work.
$ make test
$ brew install entr $ make watch
Please see CONTRIBUTING for details.
If you discover any security related issues, please email firstname.lastname@example.org instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.