PHP SSL Proxy Examples
Complete guide with production-ready example for configuring SSL proxy in PHP using cURL with environment variables and Docker support
What is an SSL Proxy?
An SSL proxy is a proxy server that handles encrypted HTTPS connections. When you make HTTP requests through an SSL proxy, the proxy terminates the SSL connection, processes the request, and establishes a new SSL connection to the target server.
Use Case: SSL proxies are commonly used for security compliance, IP whitelisting, and maintaining consistent IP addresses for API access.
Production SSL Proxy Setup
1. Setup
Create app.php:
<?php
// Check for required environment variables
$proxyHost = getenv('PROXY_HOST');
$proxyPort = getenv('PROXY_PORT');
$proxyUser = getenv('PROXY_USER');
$proxyPass = getenv('PROXY_PASS');
if ($proxyHost === false || $proxyPort === false || $proxyUser === false || $proxyPass === false) {
die("Error: PROXY_HOST, PROXY_PORT, PROXY_USER, and PROXY_PASS environment variables must be set.\n");
}
// Get your local ip
$url = 'https://outboundgateway.com/ip/';
// Construct the full proxy URL from host and port
$proxyUrl = $proxyHost . ':' . $proxyPort;
// --- Initialize cURL session ---
$ch = curl_init();
// --- Set cURL Options ---
// Set the URL to request
curl_setopt($ch, CURLOPT_URL, $url);
// Set the proxy URL to your REMOTE HTTPS proxy
curl_setopt($ch, CURLOPT_PROXY, $proxyUrl);
// Tell cURL to connect to the proxy using HTTPS
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTPS);
// Set the proxy authentication
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyUser . ':' . $proxyPass);
// Tell cURL to use Basic authentication for the proxy
curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
// Return the response as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
echo "Attempting to connect to {$url} through direct HTTPS proxy {$proxyUrl}...\n\n";
// --- Execute the cURL session ---
$response = curl_exec($ch);
// --- Check for errors and display the result ---
if (curl_errno($ch)) {
echo "cURL Error: " . curl_error($ch) . "\n";
} else {
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo "--- Success! ---\n";
echo "Status: {$httpCode}\n";
echo "Response Body:\n";
$json_data = json_decode($response, true);
echo json_encode($json_data, JSON_PRETTY_PRINT) . "\n";
}
// --- Close the cURL session ---
curl_close($ch);
?>
2. Running Locally
Note: Use single quotes for passwords with special characters (!, @, :) to prevent shell interpretation.
# Set variables and run (Linux/macOS)
export PROXY_HOST="eu-01.outboundgateway.com"
export PROXY_PORT="8443"
export PROXY_USER="your_username"
export PROXY_PASS='your_complex_password!' # Use single quotes
php app.php
3. Running with Docker
Create Dockerfile:
# Use the official PHP CLI image with Alpine Linux
FROM php:cli-alpine
WORKDIR /app
# Copy the PHP script into the container
COPY app.php .
# Command to run the PHP script when the container starts
CMD ["php", "app.php"]
Build and Run:
# 1. Build the image
docker build -t php-proxy-app .
# 2. Run the container (Linux/macOS)
docker run --rm \
-e PROXY_HOST="eu-01.outboundgateway.com" \
-e PROXY_PORT="8443" \
-e PROXY_USER="your_username" \
-e PROXY_PASS='your_complex_password!' \
php-proxy-app
Note: Replace your_username and your_complex_password! with your actual credentials.
SSL Proxy Best Practices
1. Always Use HTTPS
Never send credentials or sensitive data over unencrypted connections when using proxies.
2. Verify SSL Certificates
Always verify SSL certificates in production. Disable verification only for development environments.
3. Use Connection Timeouts
Set appropriate connection and read timeouts to prevent hanging connections.
4. Handle Errors Gracefully
Implement proper error handling for proxy authentication failures, SSL errors, and connection timeouts.
5. Use Environment Variables
Store proxy credentials in environment variables, not in code.
Common Issues and Solutions
SSL Certificate Verification Failed
Solution: Update CA certificates or use proper certificate bundle. For development only, disable verification.
407 Proxy Authentication Required
Solution: Verify proxy credentials and ensure proper authentication format (username:password).
Connection Timeout
Solution: Increase timeout values or check network connectivity to proxy server.
Memory Issues with Large Responses
Solution: Use streaming for large responses or increase PHP memory limits.
Ready to Use SSL Proxy in PHP?
Get your static SSL proxy IPs today and start making secure HTTPS connections with consistent IP addresses for compliance and security requirements.
Get Started with OutboundGatewayRelated SSL Proxy Guides
Python SSL Proxy
Complete Python examples using requests, urllib3, and aiohttp libraries.
View Python Guide →Node.js SSL Proxy
JavaScript/Node.js implementation with axios, fetch, and undici libraries.
View Node.js Guide →📖 Language Comparison: Looking for the best approach? Python offers excellent libraries like requests and aiohttp, while Node.js provides native async support. Java is ideal for enterprise applications, PHP works well for web applications, and cURL is perfect for shell scripting.