Using a proxy server with get_contents file

I am getting data for my application from a website, say x.com. I am using php's file_get_contents () function to retrieve data. Sure my server IP will show up in x.com logs. Is there a way to hide my server IP without using a proxy?

If I have a proxy, how do I use it with the get_contents () file ?

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

+3


source to share


2 answers


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);

      

+20


source


Definitely agree with farmer1992.

For those who have problems with file_get_contents

SSL + proxy, there is a known bug with PHP stream_context_create:

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



Fortunately, the workaround is simple. Basically the context creator gets confused when parsing the target https url for both proxies and SSL configs. You just need to set SNI_server_name in your 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)

      

Hope this helps someone!

0


source







All Articles