file_get_contents with https requests via proxy

clarkk picture clarkk · Jul 26, 2015 · Viewed 9.3k times · Source

How to do HTTPS requests via proxy server

The proxy server is tinyproxy on debian

code

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

echo file_get_contents('https://www.google.com', false, $context);

error

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(https://www.google.com): failed to open stream: Cannot connect to HTTPS server through proxy in C:\wamp\www\google\test.php on line 10

Answer

Viacheslav Sabirov picture Viacheslav Sabirov · Jun 7, 2016

The problem which causes your second warning can be fixed by changing 'http' in stream_context_create to 'https'