6

I am getting data for my application from a website, say x.com. I use the php function file_get_contents() to fetch the data. It is sure that , my server's ip address will be shown in x.com 's logs. Is there any way to hide my server's ip without using proxy?

If I have a proxy ,how to use it with file_get_contents() ?

I need to send the request in both HTTP POST and HTTP GET methods

7
  • 1
    file_get_contents take a param stream_or_context with it you can put your proxy into it see this php.net/manual/en/context.http.php Commented Jan 26, 2013 at 8:32
  • i tried that ,but i got the following error that the content length is not specified .So I gave the arguments content length and offset .But still it is not working . Commented Jan 26, 2013 at 8:34
  • 1
    the server side always know your ip or it cant exchange data with you (i mean http). in some case you can put a header like X-Forward-For: fakeip to tell server your fakeip, however the server side would determine which ip to use Commented Jan 26, 2013 at 8:34
  • @J what kind of proxy do you use Commented Jan 26, 2013 at 8:35
  • 1
    @J trying to make one for ya Commented Jan 26, 2013 at 8:38

2 Answers 2

25

test.php using http://ifconfig.me/ip

code modified from http://www.php.net/manual/en/function.file-get-contents.php

<?php

// Create a stream
$opts = array(
        'http'=>array(
            'method'=>"GET",
            'header'=>"Accept-language: en\r\n" .
            "Cookie: foo=bar\r\n",
            'proxy' => 'tcp://221.176.14.72:80',
            )
);

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$file = file_get_contents('http://ifconfig.me/ip', false, $context);

var_dump($file);
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks a lot..It is working..For post request I have to provide content type and length, right?
I tested it on my server..and requested for a file on the same server and checked the logs..In the logs my server's ip is visible :(
it seems that proxy server added your ip to X-Forward-For and your server used it
hiding based on how your server side read your ip. you can make a customized way if you know it
|
1

Definitely agree with farmer1992.

For anyone having issues with file_get_contents over SSL + proxy, there is a known bug with PHP's stream_context_create:

https://bugs.php.net/bug.php?id=63519

Fortunately, the workaround is simple. Basically, the context creator gets confused when parsing an "https" target URL to both the proxy and SSL configs. You just have to set the SNI_server_name in the SSL config:

$targetUrl = "https://something.com";
$sniServer = parse_url($targetUrl, PHP_URL_HOST);
$params = array('your'=>'post','params'=>'here');
$ctxConfig = array(
    'http' => array(
        'method' => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded'."\r\n",
        'content' => http_build_query($params),
        'proxy' => 'tcp://12.34.56.78:3128',
        'request_fulluri' => true
    ),
    'ssl' => array( 
        'SNI_enabled' => true,
        'SNI_server_name' => $sniServer
    )
);
$context = stream_context_create($ctxConfig);
file_get_contents($targetUrl,false,$context)

Hopefully that saves someone some time!

3 Comments

Thanks. $params is just the POST params you intend to send to the server. I added a clarifying declaration above.
undefined variable $domain
Thanks. I've corrected this above. What was $domain is now $sniServer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.