项目作者: OpenCageData

项目描述 :
PHP library to access the OpenCage geocoding API
高级语言: PHP
项目地址: git://github.com/OpenCageData/php-opencage-geocode.git
创建时间: 2014-04-23T09:06:35Z
项目社区:https://github.com/OpenCageData/php-opencage-geocode

开源协议:BSD 2-Clause "Simplified" License

下载


OpenCage Geocoding API Library for PHP

A PHP library to use the OpenCage geocoding API.

Build Status / Code Quality

Build Status
PHP version
Mastodon Follow

Overview

This library attempts to use the CURL
extension to access the OpenCage Geocoding API. If CURL support is not available, the
library falls back to using fopen wrappers.

To use the library you must either have the CURL extension compiled into your version
of PHP. Alternatively configure the use of fopen wrappers via the allow_url_fopen
directive in your php.ini.

Authentication

You need an API key, which you can sign up for here.

Tutorial

You can find a comprehensive tutorial for using this module on the OpenCage site.

Installation

With Composer

The recommended - and easiest way - to install is via Composer.
Require the library in your project’s composer.json file.

  1. $ composer require opencage/geocode

Import the Geocoder class.

  1. require "vendor/autoload.php";

The old fashioned way

See the file demo/geocode.php

Geocoding

  1. $geocoder = new \OpenCage\Geocoder\Geocoder('YOUR-API-KEY');
  2. $result = $geocoder->geocode('82 Clerkenwell Road, London');
  3. print_r($result);

Reverse geocoding

  1. $geocoder = new \OpenCage\Geocoder\Geocoder('YOUR-API-KEY');
  2. $result = $geocoder->geocode('43.831,4.360'); # latitude,longitude (y,x)
  3. print $result['results'][0]['formatted'];
  4. // 3 Rue de Rivarol, 30020 Nîmes, France

Set optional parameters

See the full list at:
https://opencagedata.com/api#optional-params

  1. $result = $geocoder->geocode('6 Rue Massillon, 30020 Nîmes, France', [
  2. 'language' => 'fr',
  3. 'countrycode' => 'fr'
  4. ]);
  5. if ($result && $result['total_results'] > 0) {
  6. $first = $result['results'][0];
  7. print $first['geometry']['lng'] . ';' . $first['geometry']['lat'] . ';' . $first['formatted'] . "\n";
  8. // 4.360081;43.8316276;6 Rue Massillon, 30020 Nîmes, Frankreich
  9. }

Set a proxy URL

  1. $geocoder->setProxy('http://proxy.example.com:1234');
  2. $result = $geocoder->geocode("Brandenburger Tor, Berlin");
  3. print_r($result['results'][0]['formatted']);
  4. // Brandenburger Tor, Unter den Linden, 10117 Berlin, Germany
  5. print_r($result['results'][0]['geometry']);
  6. // Array
  7. // (
  8. // [lat] => 52.5166047
  9. // [lng] => 13.3809897
  10. // )

Example results

  1. Array
  2. (
  3. [total_results] => 2
  4. [status] => Array
  5. (
  6. [message] => OK
  7. [code] => 200
  8. )
  9. [results] => Array
  10. (
  11. [0] => Array
  12. (
  13. [annotations] => Array
  14. (
  15. [DMS] => Array
  16. (
  17. [lat] => 51° 31' 21.60894'' N
  18. [lng] => 0° 6' 8.95198'' E
  19. )
  20. [MGRS] => 30UYC0100511930
  21. [Maidenhead] => IO91wm75qk
  22. [Mercator] => Array
  23. (
  24. [x] => -11408.763
  25. [y] => 6680801.955
  26. )
  27. [OSGB] => Array
  28. (
  29. [easting] => 531628.199
  30. [gridref] => TQ 316 821
  31. [northing] => 182177.015
  32. )
  33. [OSM] => Array
  34. (
  35. [url] => http://www.openstreetmap.org/?mlat=51.52267&mlon=-0.10249#map=17/51.52267/-0.10249
  36. )
  37. [callingcode] => 44
  38. [geohash] => gcpvjemm7csmhczg9cvt
  39. [sun] => Array
  40. (
  41. [rise] => Array
  42. (
  43. [apparent] => 1452931140
  44. [astronomical] => 1452923940
  45. [civil] => 1452928800
  46. [nautical] => 1452926280
  47. )
  48. [set] => Array
  49. (
  50. [apparent] => 1452961320
  51. [astronomical] => 1452968520
  52. [civil] => 1452963660
  53. [nautical] => 1452966120
  54. )
  55. )
  56. [timezone] => Array
  57. (
  58. [name] => Europe/London
  59. [now_in_dst] => 0
  60. [offset_sec] => 0
  61. [offset_string] => 0
  62. [short_name] => GMT
  63. )
  64. [what3words] => Array
  65. (
  66. [words] => gallons.trim.tips
  67. )
  68. )
  69. [bounds] => Array
  70. (
  71. [northeast] => Array
  72. (
  73. [lat] => 51.5227563
  74. [lng] => -0.1023801
  75. )
  76. [southwest] => Array
  77. (
  78. [lat] => 51.5226042
  79. [lng] => -0.1025907
  80. )
  81. )
  82. [components] => Array
  83. (
  84. [city] => London
  85. [country] => United Kingdom
  86. [country_code] => gb
  87. [house_number] => 82
  88. [postcode] => EC1M 5RF
  89. [road] => Clerkenwell Road
  90. [state] => England
  91. [state_district] => Greater London
  92. [suburb] => Clerkenwell
  93. )
  94. [confidence] => 10
  95. [formatted] => 82 Clerkenwell Road, London EC1M 5RF, United Kingdom
  96. [geometry] => Array
  97. (
  98. [lat] => 51.52266915
  99. [lng] => -0.10248666188363
  100. )
  101. )
  102. [1] => Array
  103. (
  104. [annotations] => Array
  105. (
  106. [DMS] => Array
  107. (
  108. [lat] => 51° 30' 30.70800'' N
  109. [lng] => 0° 7' 32.66400'' E
  110. )
  111. [MGRS] => 30UXC9945410295
  112. [Maidenhead] => IO91wm42vb
  113. [Mercator] => Array
  114. (
  115. [x] => -13997.313
  116. [y] => 6678279.278
  117. )
  118. [OSGB] => Array
  119. (
  120. [easting] => 530055.544
  121. [gridref] => TQ 300 805
  122. [northing] => 180563.298
  123. )
  124. [OSM] => Array
  125. (
  126. [url] => http://www.openstreetmap.org/?mlat=51.50853&mlon=-0.12574#map=17/51.50853/-0.12574
  127. )
  128. [geohash] => gcpvj0u6yjcmwxz8bn43
  129. [sun] => Array
  130. (
  131. [rise] => Array
  132. (
  133. [apparent] => 1452931140
  134. [astronomical] => 1452923940
  135. [civil] => 1452928800
  136. [nautical] => 1452926340
  137. )
  138. [set] => Array
  139. (
  140. [apparent] => 1452961320
  141. [astronomical] => 1452968520
  142. [civil] => 1452963660
  143. [nautical] => 1452966120
  144. )
  145. )
  146. [timezone] => Array
  147. (
  148. [name] => Europe/London
  149. [now_in_dst] => 0
  150. [offset_sec] => 0
  151. [offset_string] => 0
  152. [short_name] => GMT
  153. )
  154. [what3words] => Array
  155. (
  156. [words] => thing.then.link
  157. )
  158. )
  159. [bounds] => Array
  160. (
  161. [northeast] => Array
  162. (
  163. [lat] => 51.7202301025
  164. [lng] => 0.336111992598
  165. )
  166. [southwest] => Array
  167. (
  168. [lat] => 51.2786598206
  169. [lng] => -0.523222982883
  170. )
  171. )
  172. [components] => Array
  173. (
  174. [country] => United Kingdom
  175. [county] => Greater London
  176. [state] => England
  177. [town] => London
  178. )
  179. [confidence] => 1
  180. [formatted] => London, Greater London, United Kingdom
  181. [geometry] => Array
  182. (
  183. [lat] => 51.50853
  184. [lng] => -0.12574
  185. )
  186. )
  187. )
  188. )

Copyright (c) OpenCage GmbH. See LICENSE for details.

Who is OpenCage GmbH?

We run a worldwide geocoding API and geosearch service based on open data.
Learn more about us.

We also run Geomob, a series of regular meetups for location based service creators, where we do our best to highlight geoinnovation. If you like geo stuff, you will probably enjoy the Geomob podcast.

— end —