I searched over the internet and could not find anything about stream_context_set_default() with a password protected proxy server.
Then I thought that password in basic authorization is sent in headers.
So I modified the headers with the password extracted from a CURL request and it worked perfect!!!
Here is how you do it:
First send this request to any domain (example.com) as below:
curl -v -U user:pass -x your_proxy_ip:port --url https://example.com
See the curl sent headers and I got these proxy lines to use later:
> Trying XXX.XXX.XXX.XXX...
> Connected to XXX.XXX.XXX.XXX (XXX.XXX.XXX.XXX) port YYYY (#0)
> Establish HTTP proxy tunnel to example.com:443
> Proxy auth using Basic with user 'your_username_here'
> CONNECT example.com:443 HTTP/1.1
> Host: example.com:443
> Proxy-Authorization: Basic YW1hem9uOnXXXXXXXXXXXXXXX25SMg
> User-Agent: curl/7.47.0
> Proxy-Connection: Keep-Alive
>
< HTTP/1.1 200 Connection established
<
< Proxy replied OK to CONNECT request
OK now it is time to build our custom header:
$default_opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Proxy-Authorization: Basic YW1hem9uOnXXXXXXXXXXXXXXX25SMg\r\n" .
"Proxy-Connection: Keep-Alive",
'proxy'=>"XXX.XXX.XXX.XXX:YYYY"
)
);
$default = stream_context_set_default($default_opts);
$result = file_get_contents("https://ipleak.net/json/");
print_r(json_decode($result));
And it works perfect, you get the IP of your proxy server in response!