Skip to content

Commit 582d483

Browse files
authored
Replace internal use of fopen and cURL fallback with Guzzle library (#23)
* replace internal use of fopen and cURL fallback with Guzzle library * merged all code from AbstractGeocoder into Geocoder * add type declarations to all methods * cast json_encode return to string since this input can never fail to encode * getenv can return false, make sure we receive null instead * check if array is defined before accessing elements * check that fetched JSON is array with certain required fields * new geocodeAsync method * add tests for more error cases * add tests for network issue, no results, invalid proxy * new public method geocodeReverse and geocodeReverseAsync * split tests into multiple files * user_agent variable can be marked readonly (starting PHP 8.1) * redact half the API key in error messages
1 parent ad160b4 commit 582d483

16 files changed

Lines changed: 1107 additions & 325 deletions

.github/workflows/ci.yml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,13 @@ jobs:
3535
if [ "${{ matrix.php-version }}" != "8.5" ]; then rm composer.lock; fi
3636
composer install --no-progress --no-suggest --no-interaction
3737
38-
- name: Run phpunit tests (with and without cURL)
39-
run: |
40-
./vendor/bin/phpunit --display-deprecations
41-
SKIP_CURL=1 ./vendor/bin/phpunit --display-deprecations
38+
- name: Run phpunit tests
39+
run: ./vendor/bin/phpunit --display-deprecations
4240

43-
- name: Run proxy tests (with and without cURL)
41+
- name: Run proxy tests
4442
run: |
4543
docker run -d --name=tinyproxy -p 8888:8888 monokal/tinyproxy:latest ANY
4644
PROXY=http://0.0.0.0:8888 ./vendor/bin/phpunit
47-
PROXY=http://0.0.0.0:8888 SKIP_CURL=1 ./vendor/bin/phpunit
4845
docker rm -f tinyproxy
4946
5047
- name: Run PHPCS (Code Sniffer)

CHANGES.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
unreleased
2+
=====================================================
3+
- new method geocodeAsync
4+
- new methods geocodeReverse, geocodeReverseAsync
5+
- replace internal use of fopen and cURL fallback with Guzzle library
6+
- add type declaration to all method parameters and return values
7+
- many more tests
8+
19
3.4.0 -- Sat Mar 29 2026
210
=====================================================
311
- deprecate PHP 8.1

README.md

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,8 @@ A [PHP](http://php.net/) library to use the [OpenCage geocoding API](https://ope
99
![Mastodon Follow](https://img.shields.io/mastodon/follow/109287663468501769?domain=https%3A%2F%2Fen.osm.town%2F&style=social)
1010

1111
## Overview
12-
This library attempts to use the [CURL](http://www.php.net/manual/en/book.curl.php)
13-
extension to access the OpenCage Geocoding API. If CURL support is not available, the
14-
library falls back to using [fopen wrappers](http://uk3.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen).
15-
16-
To use the library you must either have the CURL extension compiled into your version
17-
of PHP. Alternatively configure the use of fopen wrappers via the `allow_url_fopen`
18-
directive in your `php.ini`.
12+
This library uses [Guzzle](https://docs.guzzlephp.org/en/stable/) to access the
13+
OpenCage Geocoding API. Both synchronous and asynchronous requests are supported.
1914

2015
### Authentication
2116

@@ -64,7 +59,7 @@ print_r($result);
6459

6560
```php
6661
$geocoder = new \OpenCage\Geocoder\Geocoder('YOUR-API-KEY');
67-
$result = $geocoder->geocode('43.831,4.360'); # latitude,longitude (y,x)
62+
$result = $geocoder->geocodeReverse(43.831, 4.360); # latitude, longitude
6863
print $result['results'][0]['formatted'];
6964
// 3 Rue de Rivarol, 30020 Nîmes, France
7065
```
@@ -86,6 +81,33 @@ if ($result && $result['total_results'] > 0) {
8681
}
8782
```
8883

84+
### Async geocoding
85+
86+
```php
87+
$geocoder = new \OpenCage\Geocoder\Geocoder('YOUR-API-KEY');
88+
89+
// single async request
90+
$promise = $geocoder->geocodeAsync('82 Clerkenwell Road, London');
91+
$result = $promise->wait();
92+
print_r($result);
93+
94+
// concurrent requests
95+
$promises = [
96+
'london' => $geocoder->geocodeAsync('London'),
97+
'paris' => $geocoder->geocodeAsync('Paris'),
98+
'tokyo' => $geocoder->geocodeAsync('Tokyo'),
99+
];
100+
$results = \GuzzleHttp\Promise\Utils::unwrap($promises);
101+
print $results['london']['results'][0]['formatted'];
102+
print $results['paris']['results'][0]['formatted'];
103+
print $results['tokyo']['results'][0]['formatted'];
104+
105+
// async reverse geocoding
106+
$promise = $geocoder->geocodeReverseAsync(43.831, 4.360);
107+
$result = $promise->wait();
108+
print $result['results'][0]['formatted'];
109+
```
110+
89111
### Set a proxy URL
90112

91113
```php

composer.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@
1414
"OpenCage\\Geocoder\\": "src"
1515
}
1616
},
17+
"autoload-dev": {
18+
"psr-4": {
19+
"OpenCage\\Geocoder\\Test\\": "tests"
20+
}
21+
},
1722
"require": {
18-
"php": ">=8.2"
23+
"php": ">=8.2",
24+
"guzzlehttp/guzzle": "^7.0"
1925
},
2026
"require-dev": {
2127
"phpunit/phpunit": "^12.5.8",
@@ -24,13 +30,11 @@
2430
},
2531
"scripts": {
2632
"test": "phpunit --display-deprecations",
27-
"test:nocurl": "SKIP_CURL=1 phpunit --display-deprecations",
2833
"phpcs": "phpcs .",
2934
"phpstan": "phpstan analyse",
3035
"check": [
3136
"composer validate --strict",
3237
"@test",
33-
"@test:nocurl",
3438
"@phpcs",
3539
"@phpstan"
3640
]

0 commit comments

Comments
 (0)