Connecting through a proxy

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
paultfh
Forum Commoner
Posts: 31
Joined: Thu Jul 20, 2006 6:24 pm

Connecting through a proxy

Post by paultfh »

What I am trying to do is connect to my newshost (unlimited.newshosting.com(63.218.45.254 port is 119)) to download usenet headers for my site.After sending a whitelist request to Godaddy, they replied with this:
Thank you for your email. Applications that need to make https connections (port 443) will need to be made "proxy aware". This will require additional coding to varying degrees, depending on the application. You will need to know the ip address and port of the proxy server in order to correctly modify your code. The ip of the proxy server is 64.202.165.130 and connections will be made on port 3128.

You will need to use this when coding with PHP:
curl_setopt ($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
curl_setopt ($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
curl_setopt ($ch, CURLOPT_PROXY, http://64.202.165.130:3128);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
I'm not sure how I go about this.
Last edited by paultfh on Sun Jul 23, 2006 11:48 am, edited 1 time in total.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Post code.
paultfh
Forum Commoner
Posts: 31
Joined: Thu Jul 20, 2006 6:24 pm

Post by paultfh »

Code: Select all

My code:

	function connect() {
		//connect to server
		echo "Connecting to {$this->server}\n";
		$this->nntp = new Net_NNTP;
		$ret = $this->nntp->connect($this->server,$this->port,$this->account,$this->password);
		if(PEAR::isError($ret)) {
			echo "Cannot connect to server\n";
			exit;
		}

	}

Connect code:

     /**
     * Connect to the specified port. If called when the socket is
     * already connected, it disconnects and connects again.
     *
     * @param $addr string IP address or host name
     * @param $port int TCP port number
     * @param $persistent bool (optional) whether the connection is
     *        persistent (kept open between requests by the web server)
     * @param $timeout int (optional) how long to wait for data
     * @access public
     * @return mixed true on success or error object
     */
    function connect($addr, $port, $persistent = null, $timeout = null) {
        if (is_resource($this->fp)) {
            @fclose($this->fp);
            $this->fp = null;
        }
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $addr.':'.$port);
        curl_setopt ($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE); 
				curl_setopt ($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); 
				curl_setopt ($ch, CURLOPT_PROXY, "http://64.202.165.130:3128");
				curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

        if (strspn($addr, '.0123456789') == strlen($addr)) {
            $this->addr = $addr;
        } else {
            $this->addr = gethostbyname($addr);
        }
        $this->port = $port % 65536;
        if ($persistent !== null) {
            $this->persistent = $persistent;
        }
        if ($timeout !== null) {
            $this->timeout = $timeout;
        }
        $openfunc = $this->persistent ? 'pfsockopen' : 'fsockopen';
        $errno = 0;
        $errstr = '';
        if ($this->timeout) {
            $fp = $openfunc($this->addr, $this->port, $errno, $errstr, $this->timeout);
        } else {
            $fp = $openfunc($this->addr, $this->port, $errno, $errstr);
        }
        
        if (!$fp) {
            return $this->raiseError($errstr, $errno);
        }

        $this->fp = $fp;
        
        return $this->setBlocking($this->blocking);
    }
Last edited by paultfh on Mon Jul 24, 2006 1:55 pm, edited 4 times in total.
paultfh
Forum Commoner
Posts: 31
Joined: Thu Jul 20, 2006 6:24 pm

Post by paultfh »

I found an example here:

Code: Select all

[quote]<?
$URL="https://www.paypal.com";
if (isset($_GET["site"])) { $URL = $_GET["site"]; }
$ch = curl_init();
echo "URL = $URL <br>\n";
curl_setopt($ch, CURLOPT_VERBOSE, 1);
//curl_setopt ($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
curl_setopt ($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
curl_setopt ($ch, CURLOPT_PROXY,"http://64.202.165.130:3128");
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_URL, $URL);
curl_setopt ($ch, CURLOPT_TIMEOUT, 120);
$result = curl_exec ($ch);
echo "<hr><br>\n";
echo 'Errors: ' . curl_errno($ch) . ' ' . curl_error($ch) . '<br><br>';
echo "<hr><br>\n";
curl_close ($ch);
print "result - $result";
echo "<hr><br>\n";

?>[/quote]
Post Reply