A PHP Slim Framework application that proxies SpaceX API data, demonstrating modern PHP 8.3 features and API integration patterns suitable for traffic capture and replay testing.
- RESTful API: Simple proxy endpoints for SpaceX data
- Health Checks: Built-in health monitoring endpoint
- External API Integration: SpaceX API integration with error handling
- Kubernetes Ready: Complete k8s manifests with client traffic generator
- Docker Support: Multi-stage Alpine-based build for optimal security
- Unit Tests: PHPUnit test suite with mocked external API calls
- PHP 8.3 or later
- Composer
- Docker (for containerized deployment)
- Kubernetes cluster (for k8s deployment)
# Install Homebrew if not already installed
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Install PHP 8.3
brew install php@8.3
# Add PHP to your PATH (add to ~/.zshrc or ~/.bash_profile)
echo 'export PATH="/opt/homebrew/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc
# Verify installation
php --version# Download PHP 8.3 from https://www.php.net/downloads.php
# Follow the macOS installation instructions# Download and install Composer
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
# Verify installation
composer --version- Install dependencies:
cd php
composer install- Run the application:
php -S localhost:8081 index.phpThe application will start on http://localhost:8081
# Build the image
docker build -t php-server:latest .
# Run the container
docker run -d -p 8081:8081 php-server:latest# Deploy everything (server + client)
kubectl apply -f manifest.yaml
# Check deployment status
kubectl get pods -l app=php-server
# Check client generating traffic
kubectl logs -f deployment/php-client
# Access the service (port-forward)
kubectl port-forward svc/php-server 8081:80
# Test the API
curl http://localhost:8081/healthkubectl delete -f manifest.yaml| Method | Endpoint | Description | Response |
|---|---|---|---|
GET |
/health |
Health check endpoint | {"status": "ok"} |
GET |
/spacex/launches |
Proxy SpaceX latest launch data | SpaceX API response or error |
curl http://localhost:8081/healthResponse:
{
"status": "ok"
}curl http://localhost:8080/spacex/launchesResponse:
{
"id": "launch-id",
"name": "Launch Name",
"date_utc": "2024-01-01T00:00:00.000Z",
"success": true,
...
}{
"error": "API Error message"
}# Run PHPUnit tests
composer test
# Run tests with coverage
vendor/bin/phpunit --coverage-textUse the included test.http file with VS Code REST Client extension:
# Install VS Code REST Client extension
code --install-extension humao.rest-client
# Open test.http and click "Send Request" above each testphp/
├── index.php # Main Slim application
├── composer.json # Dependencies and scripts
├── Dockerfile # Multi-stage Docker build
├── Makefile # Build and Docker targets
├── manifest.yaml # Kubernetes deployment
├── test.http # REST Client test file
├── phpunit.xml # PHPUnit configuration
├── README.md # This file
├── .gitignore # Git ignore rules
└── tests/
└── ApiTest.php # PHPUnit test suite
| Variable | Default | Description |
|---|---|---|
PORT |
8080 |
Server port (not configurable in current implementation) |
slim/slim(~> 4.12) - Web frameworkguzzlehttp/guzzle(~> 7.8) - HTTP client for external API callsphpunit/phpunit(~> 10.5) - Testing framework (dev dependency)
Use Case: Initial demos, Speedscale recording, development
Components: Server + Client
kubectl apply -f manifest.yamlWhat happens:
- PHP server starts and listens on port 8080
- Client waits for server, then generates continuous traffic
- Perfect for Speedscale to record realistic traffic patterns
Use Case: Speedscale traffic replay, testing new versions
Components: Server only (no client)
# Deploy only the server deployment and service
kubectl apply -f - <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: php-server
spec:
replicas: 1
selector:
matchLabels:
app: php-server
template:
metadata:
labels:
app: php-server
spec:
containers:
- name: php-server
image: gcr.io/speedscale-demos/php-server:v1.2.4
ports:
- containerPort: 8080
name: http
---
apiVersion: v1
kind: Service
metadata:
name: php-server
spec:
type: ClusterIP
ports:
- name: http
port: 80
targetPort: 8080
selector:
app: php-server
EOFWhat happens:
- Server starts and responds to replayed requests
- No client (Speedscale replays recorded traffic)
- Server responds to replayed requests
# Find process using port 8080
lsof -i :8080
# Kill the process
kill -9 <PID># Check pod logs
kubectl logs -l app=php-server --tail=50
# Describe pod for events
kubectl describe pod -l app=php-server# Clear composer cache
composer clear-cache
# Reinstall dependencies
rm -rf vendor composer.lock
composer installMIT License - Part of Speedscale Demo Applications