what3words/w3w-php-wrapper
Composer 安装命令:
composer require what3words/w3w-php-wrapper
包简介
A PHP library to use the what3words RESTful API
README 文档
README
A PHP library to use the what3words REST API.
Overview
The what3words PHP wrapper gives you programmatic access to
- convert a 3 word address to coordinates
- convert coordinates to a 3 word address
- autosuggest functionality which takes a slightly incorrect 3 word address, and suggests a list of valid 3 word addresses
- obtain a section of the 3m x 3m what3words grid for a bounding box.
- determine the currently support 3 word address languages.
Authentication
To use this library you’ll need to obtain an API key, please visit https://what3words.com/select-plan and sign up for an account.
Installation
Composer
To use this library in your project, place the following line in your composer.json:
"what3words/w3w-php-wrapper": "3.*"
Manual
To manually include this file, place Geocoder.php into your project, and require_once("Geocoder.php")
Initialise
use What3words\Geocoder\Geocoder; use What3words\Geocoder\AutoSuggestOption; $api = new Geocoder("<Secret API Key>");
Options
You can also provide header values and referer (for restricted API keys).
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);
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'
The returned payload from the convertToCoordinates method is described in the what3words REST API documentation.
Code Example
$result = $api->convertToCoordinates("index.home.raft"); print_r($result);
Convert To 3 Word Address
Convert coordinates, expressed as latitude and longitude to a 3 word address.
The returned payload from the convertTo3wa method is described in the what3words REST API documentation.
Code Example
$result = $api->convertTo3wa(51.432393,-0.348023); print_r($result);
Available Languages
This function returns the currently supported languages. It will return the two letter code (ISO 639), and the name of the language both in that language and in English.
The returned payload from the convertTo3wa method is described in the what3words REST API documentation
Code Example
$result = $api->availableLanguages(); print_r($result);
Grid Section
Returns a section of the 3m x 3m what3words grid for a given area. The requested box must not exceed 4km from corner to corner, or a BadBoundingBoxTooBig error will be returned. Latitudes must be >= -90 and <= 90, but longitudes are allowed to wrap around 180. To specify a bounding-box that crosses the anti-meridian, use longitude greater than 180. Example value: 50.0, 179.995, 50.01, 180.0005.
The returned payload from the gridSection function is described in the what3words REST API documentation
Code Example
$result = $api->gridSection(39.903795, 116.384550, 39.902718, 116.383122); print_r($result);
AutoSuggest
Returns a list of 3 word addresses based on user input and other parameters.
This method provides corrections for the following types of input error:
- typing errors
- spelling errors
- misremembered words (e.g. singular vs. plural)
- words in the wrong order
The autoSuggest method determines possible corrections to the supplied 3 word address string based on the probability of the input errors listed above and returns a ranked list of suggestions. This method can also take into consideration the geographic proximity of possible corrections to a given location to further improve the suggestions returned.
Input 3 word address
You will only receive results back if the partial 3 word address string you submit contains the first two words and at least the first character of the third word; otherwise an error message will be returned.
Clipping and Focus
We provide various clip policies to allow you to specify a geographic area that is used to exclude results that are not likely to be relevant to your users. We recommend that you use the clipping to give a more targeted, shorter set of results to your user. If you know your user’s current location, we also strongly recommend that you use the focus to return results which are likely to be more relevant.
In summary, the clip policy is used to optionally restrict the list of candidate AutoSuggest results, after which, if focus has been supplied, this will be used to rank the results in order of relevancy to the focus.
The returned payload from the autosuggest method is described in the what3words REST API documentation.
Usage
The first parameter is the partial three words, or voice data. It is followed by an array of AutoSuggestOption objects. The last parameter is the completion block. The AutoSuggestOption objects in the array are created using static convenience functions of the form:
AutoSuggestOption::fallbackLanguage("de"); AutoSuggestOption::focus($latitude, $longitude);
Code Example #1
$result = $api->autosuggest("fun.with.code"); print_r($result);
Code Example #2
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);
Code Example #3
Validate what3words
$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'
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.
print_r($api->getError());
Error values are listed in the what3words REST API documentation.
Development (using Docker)
You can see w3w-php-wrapper in action by running docker compose watch (make sure you're running this from the development directory) and open http://localhost:9000 on your browser. Any changes you make to the source code will be reloaded as you refresh your browser, just ensure you have loaded your environment variable named: W3W_API_KEY by running export W3W_API_KEY=<YOUR_API_KEY>.
$ 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"
Running tests (using Docker)
Once you've spin up the container, you can run docker exec php-server /var/www/html/vendor/bin/phpunit to execute the unit 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)
what3words/w3w-php-wrapper 适用场景与选型建议
what3words/w3w-php-wrapper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 635.59k 次下载、GitHub Stars 达 29, 最近一次更新时间为 2015 年 11 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「geocoding」 「what3words」 「3 word address」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 what3words/w3w-php-wrapper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 what3words/w3w-php-wrapper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 what3words/w3w-php-wrapper 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Maps in minutes. Powered by the Google Maps API.
Yii2 map input widget. Allows you to select geographcal coordinates via a human-friendly inteface.
GISCO Geocoding Provider for Geocoder PHP.
Module adding custom shipping attribute for what3words address
Laravel integration for Nominatim Geocoding API Client
Google Maps PHP SDK.
统计信息
- 总下载量: 635.59k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 29
- 点击次数: 29
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-11-23