W3W PHP Wrapper

A PHP library to use the what3words REST API.

This what3words PHP wrapper gives you programmatic access to the What3Words API version 3.

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, which can validate and autocorrect user input and limit it to certain geographic areas (this powers the search box on our map site). It 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 the list of all 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).

Step 1: Obtain an API Key

Sign up to obtain your free API key.

Step 2: Installation

Use Composer to integrate this library into your project, or do it manually.

                        
{
   “require”: {
    “what3words/w3w-php-wrapper”: "3.*"
   }
}
                    
                        
Add this file to your project: Geocoder.php.
                    

Step 3: Setup

require_once("vendor/autoload.php");

use What3words\Geocoder\Geocoder;
use What3words\Geocoder\AutoSuggestOption;

$api = new Geocoder("<Secret API Key>");

If you manually dropped in the file then use require_once("Geocoder.php") instead of require_once("vendor/autoload.php")

Step 4: Usage

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 'table.book.chair'

More info about convertToCoordinates, including returned results is available in the what3words REST API what3words REST API documentation.

Example: Find the words for ///filled.count.soap
$result = $api->convertToCordinates("filled.count.soap");
print_r($result);

Convert to 3 word address

To convert coordinates, expressed as latitude and longitude to a 3 word address, call convertTo3wa.

More info about convertTo3wa, including returned results is available in the what3words REST API what3words REST API documentation.

Example: Find the words for (51.520847, -0.195521).
$result = $api->convertTo3wa(51.520847, -0.195521);
print_r($result);

Auto Suggest

When presented with a 3 words address which 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 info about autosuggest, including returned results is available in the what3words REST API what3words REST API documentation.

Example: Simple basic call.
$result = $api->autosuggest("fun.with.code");
print_r($result);
Example: Limit the search to words by country: Great Britain.
$result = $api->autosuggest("fun.with.code", [AutoSuggestOption::clipToCountry("GB")]);
print_r($result);
Example: Focus on (51.4243877,-0.34745) and look for 6 results.
$result = $api->autosuggest("fun.with.code", [AutoSuggestOption::focus(51.4243877,-0.34745), AutoSuggestOption::numberResults(6)]);
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 info about gridSection, including returned results is available in the what3words REST API what3words REST API documentation.

Example: Get a grid for (39.903795, 116.384550) in the south west, and (39.902718, 116.383122) in the north east.
$result = $api->gridSection(39.903795, 116.384550, 39.902718, 116.383122);
print_r($result);

Available Languages

The What3Words system is available in a number of languages. More languages are added all the time, and you ask the API for the current list by calling availableLanguages. More info about availableLanguages, including returned results is available in the what3words REST API what3words REST API documentation.

Example:
$result = $api->availableLanguages();
print_r($result);

Handling Errors

All the functions will return an stdClass Object containing the requested data, or on failure, they will return false. If false is returned, call get_error(), and it will return an associative array containing a code value and a message value.

Example:
$result = $api->convertToCoordinates("garbage.input");
if ($result == false)
  {
  $error = $api->getError();
  print "Error with code " . $error["code"] . ": ";
  print $error["message"] . ":\n";
  exit();
  }

Step 5: Putting It All Together

Complete Composer convert to 3wa Example:
<?php

require_once("vendor/autoload.php");

use What3words\Geocoder\Geocoder;
use What3words\Geocoder\AutoSuggestOption;

$api = new Geocoder("<Your Secret API Key>");

$options = [
  AutoSuggestOption::clipToCountry("FR"),
  AutoSuggestOption::focus(48.856618, 2.3522411),
  AutoSuggestOption::numberResults(1)
  ];

$result = $api->autosuggest("inder.dweii.coo", $options);

if ($result)
  {
  print "Top 3 word address match: " . $result["suggestions"][0]["words"];
  print " in " . $result["suggestions"][0]["nearest-place"] . "\n";
  }
else
  print "Error with code " . $error["code"] . ": " . $error["message"] . ":\n";
  
?>
3wa Manual Example with Language and Convert To 3wa calls:
<?php

use What3words\Geocoder\Geocoder;
use What3words\Geocoder\AutoSuggestOption;

require_once("Geocoder.php");

$api = new Geocoder("<Your Secret API Key>");

$result = $api->availableLanguages();

print "Available Languages: ";
foreach ($result["languages"] as $language)
  print $language["name"] . " ";

print "\n";

// find words from coords
$result = $api->convertTo3wa(51.432393,-0.348023);

$words = $result["words"];

print "The words for (51.432393,-0.348023) are " . $words . "\n";

// find the coordinates back from those words
$result = $api->convertToCoordinates($words);

print "The coordinates for $words are (" . $result["coordinates"]["lat"] . "),(" . $result["coordinates"]["lng"] . ")\n";

?>