geocoder-php/common-http
Common HTTP layer for Geocoder PHP providers. Includes shared HTTP client abstractions, request/response handling, and helpers to integrate PSR-18 clients and PSR-7 messages, keeping geocoding providers lightweight and consistent across transports.
Illuminate\Http\Client, Guzzle, or Symfony HTTP Client). This reduces vendor lock-in and simplifies future migrations.php-geocoder package, it fits seamlessly into Laravel applications requiring geospatial features (e.g., location-based services, distance calculations, or IP geolocation).HttpClient facade or custom PSR-18 clients).Cache::remember) to store geocoding responses, reducing API calls.use Geocoder\Provider\OpenStreetMapProvider;
use Geocoder\HttpAdapter\CommonHttpAdapter;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
$httpClient = new \GuzzleHttp\Client(); // or Laravel's HttpClient
$requestFactory = new \Nyholm\Psr7\Factory\Psr17Factory();
$adapter = new CommonHttpAdapter($httpClient, $requestFactory);
$geocoder = new Geocoder\Geocoder();
$geocoder->registerProvider('osm', new OpenStreetMapProvider($adapter));
Geocoder::geocode('Paris')) for consistency with Laravel’s conventions..env for API keys) via Laravel’s config() helper.HttpClient). While Laravel’s HttpClient is PSR-18-compliant, custom implementations may need validation.^8.0 in composer.json).setMessageFactory) may require updates to custom providers. Test thoroughly if extending the package.HttpClient supports retries, but custom providers may need additional handling.Geocoder\Exception\UnsupportedProviderException).AbstractHttpProvider can be extended, but ensure compatibility with its refactored methods (e.g., getRequest()).HttpClient mocking or Mockery with PSR-18 interfaces.Log facade or a monitoring tool (e.g., Sentry).HttpClient facade, Illuminate\Support\Facades\Http).Cache::remember) to store responses (e.g., 1-hour TTL for static locations).GeocodeJob) for async processing.Geocoded, GeocodeFailed) for observability.Rule::requiredIf) to sanitize input addresses.geocoder-php stack.// Before: Direct Guzzle call
$response = Http::get('https://api.openstreetmap.org/...');
// After: Using geocoder-php
$geocoder = app(Geocoder::class);
$result = $geocoder->geocode('Paris');
AppServiceProvider:
$this->app->bind(ClientInterface::class, function ($app) {
return new \GuzzleHttp\Client();
});
Cache::remember).geocoder-php/common-http:^4.4).json, curl, or file_get_contents.Schema::create('user_locations', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained();
$table->string('address');
$table->decimal('latitude', 10, 8);
$table->decimal('longitude', 11, 8);
$table->timestamps();
});
### **Sequencing**
1. **Prerequisites**:
- Upgrade to **PHP 8.0+** and Laravel 8+ if not already.
- Install PSR-18 HTTP client (e.g., `guzzlehttp/guzzle` or `symfony/http-client`).
2. **Core Setup**:
- Install packages:
```bash
composer require geocoder-php/geocoder geocoder-php/common-http guzzlehttp/guzzle
```
- Configure providers in `config/services.php`:
```
How can I help you explore Laravel packages today?