PHP SSL Proxy Examples
Complete guide with working examples for configuring SSL proxy in PHP using cURL, Guzzle, and Symfony HTTP Client libraries
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.
PHP cURL (Built-in)
Basic SSL Proxy Configuration
<?php
// Basic SSL proxy configuration using cURL
$proxyHost = 'proxy.outboundgateway.com';
$proxyPort = 443;
$proxyUser = 'your_username';
$proxyPass = 'your_password';
// Target URL
$url = 'https://httpbin.org/ip';
// Initialize cURL session
$ch = curl_init();
// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// Set proxy
curl_setopt($ch, CURLOPT_PROXY, $proxyHost . ':' . $proxyPort);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
// Set proxy authentication
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyUser . ':' . $proxyPass);
// SSL settings (for development only - remove in production)
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
// Timeouts
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
// Execute request
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (curl_errno($ch)) {
echo 'cURL Error: ' . curl_error($ch);
} else {
echo "HTTP Code: $httpCode\n";
echo "Response: $response";
}
// Close cURL session
curl_close($ch);
?>
Advanced SSL Configuration
<?php
// Advanced SSL proxy configuration with proper certificate verification
$proxyHost = 'proxy.outboundgateway.com';
$proxyPort = 443;
$proxyUser = 'your_username';
$proxyPass = 'your_password';
// Target URL
$url = 'https://httpbin.org/ip';
// Initialize cURL session
$ch = curl_init();
// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// Set proxy
curl_setopt($ch, CURLOPT_PROXY, $proxyHost . ':' . $proxyPort);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
// Set proxy authentication
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyUser . ':' . $proxyPass);
// SSL settings (production-ready)
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
// Set CA certificate path (adjust for your system)
curl_setopt($ch, CURLOPT_CAINFO, '/etc/ssl/certs/ca-certificates.crt');
// Custom headers
$headers = [
'User-Agent: MyPHPApp/1.0',
'Accept: application/json',
'Content-Type: application/json'
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Timeouts
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
// Execute request
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (curl_errno($ch)) {
echo 'cURL Error: ' . curl_error($ch);
} else {
echo "HTTP Code: $httpCode\n";
echo "Response: $response";
}
// Close cURL session
curl_close($ch);
?>
Guzzle HTTP Client
Basic SSL Proxy Configuration
Composer Installation: Add Guzzle to your project
composer require guzzlehttp/guzzle
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
// Proxy configuration
$proxyHost = 'proxy.outboundgateway.com';
$proxyPort = 443;
$proxyUser = 'your_username';
$proxyPass = 'your_password';
// Create Guzzle client with proxy
$client = new Client([
'proxy' => [
'http' => "http://$proxyUser:$proxyPass@$proxyHost:$proxyPort",
'https' => "http://$proxyUser:$proxyPass@$proxyHost:$proxyPort",
],
'verify' => false, // Only for development
'timeout' => 30,
'connect_timeout' => 15,
'headers' => [
'User-Agent' => 'MyPHPApp/1.0',
'Accept' => 'application/json',
]
]);
try {
// Make request
$response = $client->get('https://httpbin.org/ip');
echo "Status Code: " . $response->getStatusCode() . "\n";
echo "Response Body: " . $response->getBody() . "\n";
} catch (RequestException $e) {
echo "Request failed: " . $e->getMessage() . "\n";
if ($e->hasResponse()) {
echo "Response Body: " . $e->getResponse()->getBody() . "\n";
}
}
?>
Advanced SSL Configuration
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
// Proxy configuration
$proxyHost = 'proxy.outboundgateway.com';
$proxyPort = 443;
$proxyUser = 'your_username';
$proxyPass = 'your_password';
// Create handler stack with middleware
$stack = HandlerStack::create();
// Add retry middleware
$stack->push(Middleware::retry(function ($retries, $request, $response, $exception) {
return $retries < 3 && ($exception || $response->getStatusCode() >= 500);
}, function ($delay) {
return $delay * 1000; // Convert to milliseconds
}));
// Add logging middleware
$stack->push(Middleware::log(
$logger,
new \GuzzleHttp\MessageFormatter('{method} {uri} - {code} - {req_body}')
));
// Create Guzzle client with advanced configuration
$client = new Client([
'handler' => $stack,
'proxy' => [
'http' => "http://$proxyUser:$proxyPass@$proxyHost:$proxyPort",
'https' => "http://$proxyUser:$proxyPass@$proxyHost:$proxyPort",
],
'verify' => '/etc/ssl/certs/ca-certificates.crt', // Production SSL verification
'timeout' => 30,
'connect_timeout' => 15,
'headers' => [
'User-Agent' => 'MyPHPApp/1.0',
'Accept' => 'application/json',
],
'allow_redirects' => [
'max' => 5,
'strict' => true,
'referer' => true,
'protocols' => ['https'],
'track_redirects' => true
]
]);
try {
// Make POST request with data
$response = $client->post('https://httpbin.org/post', [
'json' => [
'message' => 'Hello from PHP with SSL proxy!',
'timestamp' => date('c')
]
]);
echo "Status Code: " . $response->getStatusCode() . "\n";
echo "Response Body: " . $response->getBody() . "\n";
} catch (RequestException $e) {
echo "Request failed: " . $e->getMessage() . "\n";
if ($e->hasResponse()) {
echo "Response Body: " . $e->getResponse()->getBody() . "\n";
}
}
?>
Symfony HTTP Client
Basic SSL Proxy Configuration
Composer Installation: Add Symfony HTTP Client to your project
composer require symfony/http-client nyholm/psr7
<?php
require 'vendor/autoload.php';
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpClient\Exception\ClientException;
// Proxy configuration
$proxyHost = 'proxy.outboundgateway.com';
$proxyPort = 443;
$proxyUser = 'your_username';
$proxyPass = 'your_password';
// Create HTTP client with proxy
$client = HttpClient::create([
'proxy' => "http://$proxyUser:$proxyPass@$proxyHost:$proxyPort",
'verify_peer' => false, // Only for development
'verify_host' => false, // Only for development
'timeout' => 30,
'max_redirects' => 5,
'headers' => [
'User-Agent' => 'MyPHPApp/1.0',
'Accept' => 'application/json',
]
]);
try {
// Make GET request
$response = $client->request('GET', 'https://httpbin.org/ip');
echo "Status Code: " . $response->getStatusCode() . "\n";
echo "Content Type: " . $response->getHeaders()['content-type'][0] . "\n";
echo "Response Body: " . $response->getContent() . "\n";
// Make POST request
$postResponse = $client->request('POST', 'https://httpbin.org/post', [
'json' => [
'message' => 'Hello from Symfony HTTP Client!',
'timestamp' => date('c')
]
]);
echo "POST Status Code: " . $postResponse->getStatusCode() . "\n";
echo "POST Response: " . $postResponse->getContent() . "\n";
} catch (ClientException $e) {
echo "Client Error: " . $e->getMessage() . "\n";
echo "Response: " . $e->getResponse()->getContent(false) . "\n";
} catch (\Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
?>
Async Requests
<?php
require 'vendor/autoload.php';
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpClient\AsyncResponse;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
// Proxy configuration
$proxyHost = 'proxy.outboundgateway.com';
$proxyPort = 443;
$proxyUser = 'your_username';
$proxyPass = 'your_password';
// Create HTTP client
$client = HttpClient::create([
'proxy' => "http://$proxyUser:$proxyPass@$proxyHost:$proxyPort",
'verify_peer' => false, // Only for development
'verify_host' => false, // Only for development
'timeout' => 30,
]);
try {
// Create multiple concurrent requests
$responses = [
'ip' => $client->request('GET', 'https://httpbin.org/ip'),
'user-agent' => $client->request('GET', 'https://httpbin.org/user-agent'),
'headers' => $client->request('GET', 'https://httpbin.org/headers'),
];
// Process responses
foreach ($responses as $name => $response) {
echo "$name:\n";
echo "Status: " . $response->getStatusCode() . "\n";
echo "Content: " . $response->getContent() . "\n\n";
}
// Use async streams for better performance
$stream = $client->stream($responses);
foreach ($stream as $response => $chunk) {
if ($chunk->isLast()) {
echo "Completed: " . $response->getInfo('url') . "\n";
}
}
} catch (TransportExceptionInterface $e) {
echo "Transport Error: " . $e->getMessage() . "\n";
}
?>
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 Modern HTTP Clients
Prefer Guzzle or Symfony HTTP Client over raw cURL for better features and easier handling.
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.