yahaaylabs/gis.ph-sdk-php
PHP SDK for integrating with the GIS Philippines (gis.ph) API. Provides simple client methods to call endpoints, handle authentication, and work with responses, making it easier to add GIS.ph services to PHP/Laravel apps.
Installation
composer require yahaaylabs/gis.ph-sdk-php
Add the SDK to your config/services.php:
'gisph' => [
'api_key' => env('GISPH_API_KEY'),
'base_url' => env('GISPH_BASE_URL', 'https://api.gis.ph/v1'),
],
First Use Case: Fetching a Location Inject the SDK into a service/controller:
use YahaayLabs\GisPhSdk\GisPhClient;
public function __construct(protected GisPhClient $gisPh)
{
}
public function getLocation($address)
{
return $this->gisPh->geocode($address);
}
Environment Variables
Add to .env:
GISPH_API_KEY=your_api_key_here
Geocoding (Address → Coordinates)
$response = $gisPh->geocode('123 Main St, Manila');
$latitude = $response->getLatitude();
$longitude = $response->getLongitude();
Reverse Geocoding (Coordinates → Address)
$response = $gisPh->reverseGeocode(14.5995, 120.9842);
$formattedAddress = $response->getFormattedAddress();
Distance Matrix (Between Locations)
$origins = ['123 Main St, Manila', '456 Binondo St, Manila'];
$destinations = ['789 Makati Ave, Makati'];
$matrix = $gisPh->distanceMatrix($origins, $destinations);
Integration with Eloquent Models
use Illuminate\Database\Eloquent\Model;
use YahaayLabs\GisPhSdk\GisPhClient;
class Store extends Model
{
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->gisPh = app(GisPhClient::class);
}
public function getCoordinatesAttribute()
{
$response = $this->gisPh->geocode($this->address);
return [
'lat' => $response->getLatitude(),
'lng' => $response->getLongitude(),
];
}
}
Batch Processing
$addresses = ['Address 1', 'Address 2', 'Address 3'];
$results = collect($addresses)->map(fn($addr) => $gisPh->geocode($addr));
Wrap API calls in a try-catch:
try {
$response = $gisPh->geocode($address);
} catch (\YahaayLabs\GisPhSdk\Exceptions\GisPhException $e) {
Log::error('GISPH Error: ' . $e->getMessage());
return response()->json(['error' => 'Location not found'], 404);
}
Rate Limiting
$retryCount = 0;
while ($retryCount < 3) {
try {
return $gisPh->geocode($address);
} catch (\YahaayLabs\GisPhSdk\Exceptions\RateLimitException $e) {
sleep(2 ** $retryCount);
$retryCount++;
}
}
API Key Leaks
Floating-Point Precision
14.5995123456789. Round for storage:
$roundedLat = round($response->getLatitude(), 6);
Timeouts
config/services.php:
'gisph' => [
'timeout' => 60, // seconds
],
Enable Debug Mode
Set debug: true in config/services.php to log raw API responses:
'gisph' => [
'debug' => env('GISPH_DEBUG', false),
],
Mocking for Tests Use Laravel's HTTP mocking:
$mock = Mockery::mock('overload:' . GisPhClient::class);
$mock->shouldReceive('geocode')
->with('123 Main St')
->andReturn(new GisPhResponse(14.5995, 120.9842));
Custom Response Handling Extend the base response class:
namespace App\Services;
use YahaayLabs\GisPhSdk\GisPhResponse;
class CustomGisPhResponse extends GisPhResponse
{
public function getBarangay()
{
return $this->getComponents()['barangay'] ?? null;
}
}
Override the SDK's response factory in a service provider:
public function register()
{
$this->app->bind(GisPhResponse::class, CustomGisPhResponse::class);
}
Caching Responses Cache geocoding results for 1 hour:
public function getCoordinates($address)
{
return Cache::remember("gisph_{$address}", now()->addHours(1), function() use ($address) {
return $this->gisPh->geocode($address);
});
}
Fallback Mechanisms Combine with other APIs (e.g., Google Maps) if GIS.PH fails:
public function resolveLocation($address)
{
try {
return $this->gisPh->geocode($address);
} catch (\Exception $e) {
return $this->fallbackGeocoder->geocode($address);
}
}
How can I help you explore Laravel packages today?