Smith
Smith

Reputation: 3

file_get_contents via proxy

I want to read some pages/sites from the Internet by using file_get_contents and proxy. I came up with the following code:

$opts = array('http' => array('proxy' => '14.199.56.205:8909', 'request_fulluri' => true));

$context = stream_context_create($opts);

$test = file_get_contents('http://www.google.com', false, $context);

echo $test;

I took proxy from list located here http://www.hidemyass.com/proxy-list/

I tested proxy and it is working from browser, but with file_get_contents i just receive blank page.

Where is mistake? :)

Upvotes: 0

Views: 2807

Answers (2)

Vassilis Kotaras
Vassilis Kotaras

Reputation: 1

Nowadays most sites use HTTPS. Therefore, in your $opts variable you should use 'HTTPS' and not 'HTTP'.

Upvotes: 0

Corbin
Corbin

Reputation: 373

Free proxies are hit or miss and regularly fail for one reason or another. Here is a function I use that will randomly try 2 proxies from an array of proxies looking for HTTP 200. As a last resort it uses anonymouse.org to get the file.

function proxy($url) {

    $proxies = array(); 
    $proxies[] = '1.1.1.1:80';
    $proxies[] = '1.1.1.1:80';
    $proxies[] = '1.1.1.1:80';
    $proxies[] = '1.1.1.1:80';
    $proxies[] = '1.1.1.1:80';
    $proxies[] = '1.1.1.1:80';

    $http=0;
    $try=0;
    while (true) {
        $proxy = $proxies[array_rand($proxies)];
        if (!function_exists('curl_init')) { die('Sorry cURL is not installed!'); }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_REFERER, "http://www.yomamma.com/");
        curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_PROXY, $proxy);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        $output = curl_exec($ch);
        $http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        if ($http==200) { break; }
        $try++;
        if($try>2) { break; }
    }

    if ($http!=200) {
        $output=file_get_contents("http://anonymouse.org/cgi-bin/anon-www.cgi/$url");
    } 

    return $output;

}

Upvotes: 0

Related Questions