Use fsockopen with proxy

nacholibre picture nacholibre · Jun 4, 2012 · Viewed 10.2k times · Source

I have a simple whois script

    if($conn = fsockopen ($whois_server, 43)) {
        fputs($conn, $domain."\r\n");
        while(!feof($conn)) {
            $output .= fgets($conn, 128);
        }
        fclose($conn);
        return $output;
    }

$whois_server = whois.afilias.info; //whois server for info domains

but I want to run in through proxy. So I need to connect to proxy -> then connect to whois server -> and then make the request. Something like this?

$fp = fsockopen($ip,$port);

fputs($fp, "CONNECT $whois_server:43\r\n $domain\r\n");

But it doesn't work, I don't know if i'm making the second connection right.

Answer

NetworkSys Co. Ltd picture NetworkSys Co. Ltd · Nov 18, 2012

Sending your request to the proxy should do what you like:

<?php
    $proxy = "1.2.3.4"; // proxy 
    $port = 8080; // proxy port

    $fp = fsockopen($proxy,$port); // connect to proxy
    fputs($fp, "CONNECT $whois_server:43\r\n $domain\r\n");

    $data="";
    while (!feof($fp)) $data.=fgets($fp,1024);
    fclose($fp);
    var_dump($data);
?>

Rather than doing that I would recommend you using CURL in combination with:

curl_setopt($ch, CURLOPT_PROXY, 1.2.3.4);