PHP code example of what3words / w3w-php-wrapper

1. Go to this page and download the library: Download what3words/w3w-php-wrapper library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

what3words / w3w-php-wrapper example snippets


"what3words/w3w-php-wrapper": "3.*"

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

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

use What3words\Geocoder\Geocoder;
use What3words\Geocoder\GeocoderOptions;

$options = GeocoderOptions::referer("https://my.site.com/*")->headers(["X-Custom-Header: my.site.com"]);

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


$result = $api->convertToCoordinates("index.home.raft");
print_r($result);

$result = $api->convertTo3wa(51.432393,-0.348023);
print_r($result);

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

$result = $api->gridSection(39.903795, 116.384550, 39.902718, 116.383122);
print_r($result);

AutoSuggestOption::fallbackLanguage("de");
AutoSuggestOption::focus($latitude, $longitude);

$result = $api->autosuggest("fun.with.code");
print_r($result);

$result = $api->autosuggest("fun.with.code", [AutoSuggestOption::focus(51.4243877,-0.34745), AutoSuggestOption::numberResults(6)]);
print_r($result);

$result = $api->isPossible3wa("filled.count.soap");
print_r($result); // yields 1

$result = $api->isPossible3wa("not a 3wa");
print_r($result); // yields 0

$result = $api->findPossible3wa('from "index.home.raft" to " "filled.count.soap"');
print_r(implode(", ", $result)); // yields "index.home.raft, filled.count.soap"

$result = $api->isValid3wa('filled.count.soapp');
print_r($result ? $result : 'invalid'); // yields 'invalid'


print_r($api->getError());
sh
$ export W3W_API_KEY=KEYFROMW3W
$ docker compose watch
[+] Building 0.6s (18/18) FINISHED
[+] Running 1/1
 ✔ Container php-server  Started                                                                                     0.0s
Watch configuration for service "server":
  - Action sync for path "/w3w-php-wrapper/development/src"
  - Action sync for path "/w3w-php-wrapper/src"
  - Action sync for path "/w3w-php-wrapper/tests"

$ docker exec php-server /var/www/html/vendor/bin/phpunit
PHPUnit 5.5.4 by Sebastian Bergmann and contributors.

......................                                            22 / 22 (100%)

Time: 1.57 seconds, Memory: 3.25MB

OK (22 tests, 25 assertions)