Php Proxy File_Get_Contents

P

file_get_contents behind a proxy? - Stack Overflow

file_get_contents behind a proxy? – Stack Overflow

To use file_get_contents() over/through a proxy that doesn’t require authentication, something like this should do:
(I’m not able to test this one: my proxy requires an authentication)
$aContext = array(
” => array(
‘proxy’ => ‘tcp192. 168. 0. 2:3128’,
‘request_fulluri’ => true, ), );
$cxContext = stream_context_create($aContext);
$sFile = file_get_contents(“, False, $cxContext);
echo $sFile;
Of course, replacing the IP and port of my proxy by those which are OK for yours;-)
If you’re getting that kind of error:
Warning: file_get_contents() []: failed to open stream: HTTP request failed! HTTP/1. 0 407 Proxy Authentication Required
It means your proxy requires an authentication.
If the proxy requires an authentication, you’ll have to add a couple of lines, like this:
$auth = base64_encode(‘LOGIN:PASSWORD’);
‘request_fulluri’ => true,
‘header’ => “Proxy-Authorization: Basic $auth”, ), );
Same thing about IP and port, and, this time, also LOGIN and PASSWORD;-) Check out all valid options.
Now, you are passing an Proxy-Authorization header to the proxy, containing your login and password.
And… The page should be displayed;-)
file_get_contents with https requests via proxy - BotProxy

file_get_contents with https requests via proxy – BotProxy

Question
How to do HTTPS requests via proxy server
The proxy server is tinyproxy on debian
$context = stream_context_create([
” => [
‘proxy’ => ‘tcp’,
‘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:wampwwwgoogle on line 10
Warning: file_get_contents(): failed to open stream: Cannot connect to HTTPS server through proxy in C:wampwwwgoogle on line 10
Answer
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.

cc by-sa 3
HTTP context options - Manual - PHP

HTTP context options – Manual – PHP

HTTP context options — HTTP context option listing
Description
Context options for and
transports.
Options
method
string
GET, POST, or
any other HTTP method supported by the remote server.
Defaults to GET.
Additional headers to be sent during request. Values
in this option will override other values (such as
User-agent:, Host:,
and Authentication:),
even when following Location: redirects.
Thus it is not recommended to set a Host: header,
if follow_location is enabled.
user_agent
Value to send with User-Agent: header. This value will
only be used if user-agent is not specified
in the header context option above.
By default the
setting is used.
content
Additional data to be sent after the headers. Typically used
with POST or PUT requests.
proxy
URI specifying address of proxy server. (e. g.
tcp).
request_fulluri
bool
When set to true, the entire URI will be used when
constructing the request. g.
GET HTTP/1. 0).
While this is a non-standard request format, some
proxy servers require it.
Defaults to false.
follow_location
int
Follow Location header redirects. Set to
0 to disable.
Defaults to 1.
max_redirects
The max number of redirects to follow. Value 1 or
less means that no redirects are followed.
Defaults to 20.
protocol_version
float
HTTP protocol version.
Defaults to 1. 1 as of PHP 8. 0. 0;
prior to that version the default was 1. 0.
timeout
Read timeout in seconds, specified by a float
(e. g. 10. 5).
default_socket_timeout
ignore_errors
Fetch the content even on failure status codes.
Examples
Example #1 Fetch a page and send POST data
 ‘some content’, ‘var2’ => ‘doh’));$opts = array(” => array( ‘method’ => ‘POST’, ‘header’ => ‘Content-type: application/x-www-form-urlencoded’, ‘content’ => $postdata));$context = stream_context_create($opts);$result = file_get_contents(”, false, $context);? >
Example #2 Ignore redirects but fetch headers and content
array( ‘method’ => ‘GET’, ‘max_redirects’ => ‘0’, ‘ignore_errors’ => ‘1’));$context = stream_context_create($opts);$stream = fopen($url, ‘r’, false, $context);// header information as well as meta data// about the streamvar_dump(stream_get_meta_data($stream));// actual data at $urlvar_dump(stream_get_contents($stream));fclose($stream);? >
Notes
Note:
Underlying socket stream context options
Additional context options may be supported by the
underlying transport
For streams, refer to context
options for the tcp transport. For
streams, refer to context options
for the ssl transport.
HTTP status line
When this stream wrapper follows a redirect, the
wrapper_data returned by
stream_get_meta_data() might not necessarily contain
the HTTP status line that actually applies to the content data at index
0.
array (
‘wrapper_data’ =>
0 => ‘HTTP/1. 0 301 Moved Permanently’,
1 => ‘Cache-Control: no-cache’,
2 => ‘Connection: close’,
3 => ‘Location: ‘,
4 => ‘HTTP/1. 1 200 OK’,…
The first request returned a 301 (permanent redirect),
so the stream wrapper automatically followed the redirect to get a
200 response (index = 4).
(a) ¶3 years ago
note that both and transports require the same context name // OK example this will work as expected// note the url with but context with $correct_data = file_get_contents(”, false, stream_context_create(array(” => array(… ))));// INVALID example this will not work, the context will be ignored// note the url with also context with $correct_data = file_get_contents(”, false, stream_context_create(array(” => array(… ))));
nate ¶7 years ago
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 add a “Connection: close” header to the request to eliminate the keep-alive timeout: [ ‘protocol_version’ => 1. 1, ‘header’ => [ ‘Connection: close’, ], ], ]));? >
daniel dot peder at gmail dot com ¶3 years ago
note that for both and protocols require the same ” context keyword: array(… ))));
gourav sarkar ¶10 years ago
watch your case when using methods (POST and GET).. must be always uppercase. in case of you write it in lower case it wont work.
vchampion at gmail dot com ¶8 years ago
If you use the proxy server and encounter an error “fopen(): 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.
For example (working sample):
Array(“method” => “GET”,
“timeout” => 20,
“header” => “User-agent: Myagent”,
“proxy” => “tcpmy-proxy. localnet:3128”,
‘request_fulluri’ => True /* without this option we get an HTTP error! */)));
if ( $fp = fopen(“, ‘r’, false, $stream)) {
print “well done”;}? >
P>S> PHP 5. 3. 17
ywarnier at beeznest dot org ¶4 years ago
Note that setting request_fulluri to true will *change* the value of $_SERVER[‘REQUEST_URI] on the receiving end (from / to).
jay ¶6 years ago
Remember to match content with Content-type: ‘some content’, ‘var2’ => ‘doh’);$opts = array(” => array( ‘method’ => ‘POST’, ‘header’ => ‘Content-type: application/json’, // here… ‘content’ => json_encode($data) // and here. ));…? >
aruntechguy at outlook dot com ¶4 years ago
If you want to use Basic Auth while using get_headers(), use stream_context options below. I am using HEAD method here, but please feel free to use GET also. [ ‘method’ => ‘HEAD’, ‘header’ => ‘Authorization: Basic ‘. $basicAuth]]);$result = get_headers($targetUrl);print_r($result);
chris ¶7 years ago
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: array( ‘proxy’ => $proxy_string), ‘ssl’ => array( ‘SNI_enabled’ => true, ‘SNI_server_name’ => $domain));$context = stream_context_create($opts);$handle = fopen( $file, ‘r’, false, $context);? >
ASchmidt at Anamera dot net ¶7 months ago
With the default of ‘follow_location’ => 1be certain NEVER include a “Host:” header in the ‘header’ the host is set to “”, and that web site has a (quite common) redirect to “, then the initial request to ” will get the expected response of:HTTP/1. 1 301 Moved PermanentlyLocation:, the follow-up request does NOT replace the original “host” header with the new “location” value, as one would expect. Consequently each “follow-location” request will again be served by the original host of “, and continue the redirect loop until ‘max_redirects’ has been exhausted. (For details:)

Frequently Asked Questions about php proxy file_get_contents

About the author

proxyreview

If you 're a SEO / IM geek like us then you'll love our updates and our website. Follow us for the latest news in the world of web automation tools & proxy servers!

By proxyreview

Recent Posts

Useful Tools