clarkk clarkk - 4 months ago 66x
PHP Question

file_get_contents with https requests via proxy

How to do HTTPS requests via proxy server

The proxy server is

on debian


$context = stream_context_create([
'http' => [
'proxy' => 'tcp://xx.xx.xx.xx:8888',
'request_fulluri' => true

echo file_get_contents('', false, $context);


Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol in C:\wamp\www\google\test.php on line 10

Warning: file_get_contents( failed to open stream: Cannot connect to HTTPS server through proxy in C:\wamp\www\google\test.php on line 10


I would recommend cURL to do this. Somewhere on stackoverflow a user said this and I totally agree.

file_get_contents() is a simple screwdriver. Great for simple GET requests where the header, HTTP request method, timeout, cookiejar, redirects, and other important things do not matter. cURL with setopt is a powerdrills with almost every option you can think of.


$url = '';
// to check your proxy
// $url = '';
$proxy = '';

// create curl resource
$ch = curl_init();

// set options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // read more about HTTPS
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/20080311 Firefox/');

// $output contains the output string
$output = curl_exec($ch);

// close curl resource to free up system resources

echo $output;