Смотрите также

User Contributed Notes 9 notes

Note that if you set the protocol_version option to 1.1 and the server you are requesting from is configured to use keep-alive connections, the function (fopen, file_get_contents, etc.) will "be slow" and take a long time to complete. This is a feature of the HTTP 1.1 protocol you are unlikely to use with stream contexts in PHP.

Simply add a "Connection: close" header to the request to eliminate the keep-alive timeout:

If you use the proxy server and encounter an error "fopen(http://example.com): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request" note that in many situations you need also set the parameter "request_fulluri" to "true" in your stream options. Without this option the php script sends the empty request to the server as "GET / HTTP/0.0" and the proxy server replies to it with the "HTTP 400" error.

note that both http and https transports require the same context name http

// OK example:// this will work as expected// note the url with https but context with http$correct_data = file_get_contents('https://example.com', false, stream_context_create(array('http' => array(...))));

// INVALID example:// this will not work, the context will be ignored// note the url with https also context with https$correct_data = file_get_contents('https://example.com', false, stream_context_create(array('https' => array(...))));

note that for both http and https protocols require the same 'http' context keyword:

<?php

// CORRECT example:// this will work as expected// note the url with https but context with http$correct_data = file_get_contents('https://example.com', false, stream_context_create(array('http' => array(...))));

// INVALID example:// this will not work, the context will be ignored// note the url with https also context with https$correct_data = file_get_contents('https://example.com', false, stream_context_create(array('https' => array(...))));

I had quite a bit of trouble trying to make a request with fopen through a proxy to a secure url. I kept getting a 400 Bad Request back from the remote host. It was receiving the proxy url as the SNI host. In order to get around this I had to explicity set the SNI host to the domain I was trying to reach. It's apparently the issue outlined in this bug: