0
votes

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? :)

2
ur ip address does not return anything... probably thats whyVal
Grr, now is down, but it was up in time when i posted this.. Anyway now i use 77.78.3.83:9090 and its same.. I tested this proxy and it is working at the moment.Smith
so your code works, but the IP wasn't ?Val
Have you enabled error_reporting(E_ALL);? Otherwise hard to say, use curl alternatively.mario
Right now, ip work and code doesn't return anything.Smith

2 Answers

0
votes

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;

}
0
votes

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