A PHP-based Weather API that fetches and returns weather data from a 3rd party API.
PHP Weather APi is a weather API that fetches and returns weather data from Visual Crossing's API. This API uses Redis for caching and phpdotenv for loads environment variables. This project is designed to explore and practice working with the 3rd party API, Caching and Environment Variables in PHP.
- PHP - version 8.3.6
- Redis (via Predis) - version 7.4.1
- Real-Time Weather Data: Fetch up-to-date weather information for any location.
- Location-Based Queries: Retrieve weather details by specifying the desired location in the query parameter.
- Caching for Faster Responses: Utilizes Redis caching to store weather data temporarily, reducing API requests and improving response times.
- Rate Limiting: Implements rate limiting to prevent excessive API calls, ensuring fair usage and maintaining service reliability.
- JSON Responses: Returns structured JSON data, making it easy to integrate with front-end applications or other systems.
- Customizable: Easily configurable base URL and API key via environment variables
.env
, allowing seamless integration with different weather API providers.
To run this API, you’ll need:
- PHP: Version 8.3 or newer
- Redis: Version 7.4 or newer
How to install:
-
Clone the repository.
git clone https://github.com/krisnaajiep/php-weather-api.git
-
Install dependencies.
composer install
-
Configure
.env
file.cp .env.example .env
-
Configure API key and Redis connection in
.env
file.API_BASE_URL="https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/" API_KEY="" REDIS_HOST=127.0.0.1 REDIS_PASSWORD=null REDIS_PORT=6379 REDIS_CACHE_DB=0
-
Run the PHP built-in server.
php -S localhost:8000
Access the endpoint with location query parameter.
http://localhost:8000/?location=[location]
Project is: complete.
This project was inspired by roadmap.sh.