+ Post New Thread
Results 1 to 4 of 4
Web Development Thread, forward a server side web request via a proxy in Coding and Web Development; We have a school website which needs to be able to send information at the server side to paypal. The ...
  1. #1

    localzuk's Avatar
    Join Date
    Dec 2006
    Location
    Minehead
    Posts
    17,966
    Thank Post
    519
    Thanked 2,502 Times in 1,942 Posts
    Blog Entries
    24
    Rep Power
    841

    forward a server side web request via a proxy

    We have a school website which needs to be able to send information at the server side to paypal. The problem is that all traffic on http or https needs to go via our web server, something which apache obviously doesn't do.

    Is there a way of doing this in the php script itself?

  2. #2

    Geoff's Avatar
    Join Date
    Jun 2005
    Location
    Fylde, Lancs, UK.
    Posts
    11,807
    Thank Post
    110
    Thanked 583 Times in 504 Posts
    Blog Entries
    1
    Rep Power
    224
    Use curl. Consider the following code:

    PHP Code:
    $ch curl_init("http://rss.news.yahoo.com/rss/topstories");

    curl_setopt($chCURLOPT_RETURNTRANSFER1);
    curl_setopt($chCURLOPT_PROXY"http://xxx.xxx.xxx.xxx:8080");
    curl_setopt($chCURLOPT_PROXYPORT8080);
    curl_setopt ($chCURLOPT_PROXYUSERPWD"xxx:xxx");

    $x curl_exec($ch);

    print 
    "page:" htmlentities($x) . curl_error($ch) ;

    curl_close($ch); 

  3. #3

    localzuk's Avatar
    Join Date
    Dec 2006
    Location
    Minehead
    Posts
    17,966
    Thank Post
    519
    Thanked 2,502 Times in 1,942 Posts
    Blog Entries
    24
    Rep Power
    841
    As I'm editing the code of another app, I have a slight problem. This is the section of code in question:

    Code:
    $header = "POST $uri HTTP/1.0\r\n";
        $header.= "User-Agent: PHP/".phpversion()."\r\n";
        $header.= "Referer: ".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].@$_SERVER['QUERY_STRING']."\r\n";
        $header.= "Server: ".$_SERVER['SERVER_SOFTWARE']."\r\n";
        $header.= "Host: ".$hostname.":80\r\n";
        $header.= "Content-Type: application/x-www-form-urlencoded\r\n";
        $header.= "Content-Length: ".strlen($workstring)."\r\n";
        $header.= "Accept: */*\r\n\r\n";
    
    
    
        $fp = fsockopen ( $hostname, 80, $errno, $errstr, 30);
    
    //OTHER STUFF HERE
    
     fwrite($fp, $header . $workstring);
          $res = '';
          while (!feof($fp)) {
               $res .= fgets ($fp, 1024);
          }
          fclose ($fp);
    So, the problem I have is, how to pass the header info and parse the connection in line with the existing code?

  4. #4

    localzuk's Avatar
    Join Date
    Dec 2006
    Location
    Minehead
    Posts
    17,966
    Thank Post
    519
    Thanked 2,502 Times in 1,942 Posts
    Blog Entries
    24
    Rep Power
    841
    How about this? Using the code suggested on the fsockopen page on php.net:

    Code:
    <?php 
    /* 
     * 
     * No Proxy Authentification Implemented; PHP 5 
     * 
     */ 
    
    class RemoteFopenViaProxy { 
    
        private $result; 
        private $proxy_name; 
        private $proxy_port; 
        private $request_url; 
    
        public function get_proxy_name() { 
            return $this->proxy_name; 
        } 
    
        public function set_proxy_name($n) { 
            $this->proxy_name = $n; 
        } 
    
        public function get_proxy_port() { 
            return $this->proxy_port; 
        } 
    
        public function set_proxy_port($p) { 
            $this->proxy_port = $p; 
        } 
    
        public function get_request_url() { 
            return $this->request_url; 
        } 
    
        public function set_request_url($u) { 
            $this->request_url = $u; 
        } 
    
        public function get_result() { 
            return $this->result; 
        } 
    
        public function set_result($r) { 
            $this->result = $r; 
        } 
    
        private function get_url_via_proxy() { 
    
            $proxy_fp = fsockopen($this->get_proxy_name(), $this->get_proxy_port()); 
    
            if (!$proxy_fp) { 
                return false; 
            } 
            fputs($proxy_fp, "GET " . $this->get_request_url() . " HTTP/1.0\r\nHost: " . $this->get_proxy_name() . "\r\n\r\n"); 
            while (!feof($proxy_fp)) { 
                $proxy_cont .= fread($proxy_fp, 4096); 
            } 
            fclose($proxy_fp); 
            $proxy_cont = substr($proxy_cont, strpos($proxy_cont, "\r\n\r\n") + 4); 
            return $proxy_cont; 
    
        } 
    
        private function get_url($url) { 
            $fd = @ file($url); 
            if ($fd) { 
                return $fd; 
            } else { 
                return false; 
            } 
        } 
    
        private function logger($line, $file) { 
            $fd = fopen($file . ".log", "a+"); 
            fwrite($fd, date("Ymd G:i:s") . " - " . $file . " - " . $line . "\n"); 
            fclose($fd); 
        } 
    
        function __construct($url, $proxy_name = "", $proxy_port = "") { 
    
            $this->set_request_url($url); 
            $this->set_proxy_name($proxy_name); 
            $this->set_proxy_port($proxy_port); 
    
        } 
    
        public function request_via_proxy() { 
    
            $this->set_result($this->get_url_via_proxy()); 
            if (!$this->get_result()) { 
                $this->logger("FAILED: get_url_via_proxy(" . $this->get_proxy_name() . "," . $this->get_proxy_port() . "," . $this->get_request_url() . ")", "RemoteFopenViaProxyClass.log"); 
            } 
        } 
    
        public function request_without_proxy() { 
    
            $this->set_result($this->get_url($this->get_request_url())); 
            if (!$this->get_result()) { 
                $this->logger("FAILED: get_url(" . $url . ")", "RemoteFopenViaProxyClass.log"); 
            } 
        } 
    } 
    ?> 
    
    Use it this way: 
    
    <?php 
    // call constructor 
    $obj = new RemoteFopenViaProxy($insert_request_url, $insert_proxy_name, $insert_proxy_port); 
    // change settings after object generation 
    $obj->set_proxy_name($insert_proxy_name); 
    $obj->set_proxy_port($insert_proxy_port); 
    $obj->set_request_url($insert_request_url); 
    $obj->request_via_proxy(); 
    echo $obj->get_result(); 
    ?>
    Would that work?

    Changing to use curl is another option, but I'm not sure where i'd start with that in this code.

SHARE:
+ Post New Thread

Similar Threads

  1. asp server side validation
    By RabbieBurns in forum Web Development
    Replies: 15
    Last Post: 4th February 2009, 04:20 PM
  2. Replies: 6
    Last Post: 13th August 2008, 11:34 PM
  3. Forward a URL request onto another proxy server
    By FN-GM in forum How do you do....it?
    Replies: 5
    Last Post: 7th July 2008, 08:58 PM
  4. Forward Remote Desktop to a different server...
    By mrforgetful in forum Windows
    Replies: 10
    Last Post: 21st September 2007, 08:24 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •