All tutorials
PHP
This what3words PHP wrapper gives you programmatic access to the what3words API V3.
The what3words API is a fast, simple interface which allows you to convert 3 word addresses such as ///filled.count.soap to latitude and longitude coordinates such as (-0.203586, 51.521251
) and vice versa. It features a powerful AutoSuggest function that can validate and autocorrect user input and limit it to certain geographic areas (this powers the search box on our map site). The API allows you to request a section of the what3words grid (which can be requested as GeoJSON for easy display on online maps), and to request a list of all current languages supported by what3words. For advanced users, AutoSuggest can be used to post-process voice output. See links on the left to navigate.
All coordinates are latitude,longitude pairs in standard WGS-84 (as commonly used worldwide in GPS systems). All latitudes must be in the range of -90 to 90 (inclusive).
Installation
The source code is available on GitHub. Use Composer to integrate this library into your project, or do it manually.
Composer:
{ “require”: { “what3words/w3w-php-wrapper”: "3.*" } }
Manually:
Add this file to your project: Geocoder.php.
Setup
If you manually dropped in the file then use require_once("Geocoder.php")
instead of require_once("vendor/autoload.php")
require_once("vendor/autoload.php"); use What3words\Geocoder\Geocoder; use What3words\Geocoder\AutoSuggestOption; $api = new Geocoder("<Secret API Key>");
Usage
Convert to 3 word address
Convert coordinates, expressed as latitude and longitude to a 3 word address.
More info about convertToCoordinates, including returned results is available in the what3words REST API documentation.
Find the words for (51.520847, -0.195521
):
$result = $api->convertTo3wa(51.520847, -0.195521); print_r($result);
Convert to coordinates
Convert a 3 word address to a position, expressed as coordinates of latitude and longitude.
This function takes the words parameter as a string of 3 words 'filled.count.soap'
More info about convertToCoordinates, including returned results is available in the what3words REST API documentation.
Find the words for ///filled.count.soap:
$result = $api->convertToCoordinates("filled.count.soap"); print_r($result);
AutoSuggest
When presented with a 3 words address that may be incorrectly entered, AutoSuggest returns a list of potential correct 3 word addresses. It needs the first two words plus at least the first character of the third word to produce suggestions.
This method provides corrections for mis-typed words (including plural vs singular), and words being in the wrong order.
Optionally, clipping can narrow down the possibilities, and limit results to:
- One or more countries
- A geographic area (a circle, box or polygon)
This dramatically improves results, so we recommend that you use clipping if possible.
To improve results even further, set the focus
to user’s current location. This will make autosuggest
return results which are closer to the user.
More information about autosuggest
, including returned results is available in the what3words REST API documentation.
Code example Simple basic call:
$result = $api->autosuggest("fun.with.code"); print_r($result);
Code example AutoSuggest, clipping the results returned to France:
$result = $api->autosuggest("fun.with.code", [AutoSuggestOption::clipToCountry("FR")]); print_r($result);
Code example AutoSuggest, Focus on (51.4243877,-0.34745
)
$result = $api->autosuggest("fun.with.code", [AutoSuggestOption::focus(51.4243877,-0.34745)); print_r($result);
Code example AutoSuggest, with Generic Voice input type.
$result = $api->autosuggest("fun.with.code", [AutoSuggestOption::fallbackLanguage("EN"), AutoSuggestOption::inputType("generic-voice")]); print_r($result);
Grid section
Grid Section returns a section of the what3words 3m x 3m grid as a set of horizontal and vertical lines covering the requested area, which can then be drawn onto a map.
The requested box must not exceed 4km from corner to corner, or a BadBoundingBoxTooBig
error will be returned.
More information about gridSection
, including returned results is available in the what3words REST API documentation.
Get a grid for 52.207988,0.116126
) in the south-west, and 52.208867,0.117540
in the north east:
$result = $api->gridSection(39.903795, 116.384550, 39.902718, 116.383122); print_r($result);
Available languages
This function returns the currently supported languages. It will return the two letter code, and the name of the language both in that language and in English.
More information about availableLanguages, including returned results is available in the what3words REST API documentation.
$result = $api->availableLanguages(); print_r($result);
Handling errors
Errors returned from the API can be caught with the wrapper through the use of a catch
function.
Within the catch
function, code
and message
values which represent the error, are accessible from the error
object parameter
$result = $api->convertToCoordinates("garbage.input"); if ($result == false) { $error = $api->getError(); print "Error with code " . $error["code"] . ": "; print $error["message"] . ":\n"; exit(); }