localzuk Posted July 20, 2009 Posted July 20, 2009 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?
Geoff Posted July 20, 2009 Posted July 20, 2009 Use curl. Consider the following code: $ch = curl_init("http://rss.news.yahoo.com/rss/topstories"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_PROXY, "http://xxx.xxx.xxx.xxx:8080"); curl_setopt($ch, CURLOPT_PROXYPORT, 8080); curl_setopt ($ch, CURLOPT_PROXYUSERPWD, "xxx:xxx"); $x = curl_exec($ch); print "page:" . htmlentities($x) . curl_error($ch) ; curl_close($ch);
localzuk Posted July 20, 2009 Author Posted July 20, 2009 As I'm editing the code of another app, I have a slight problem. This is the section of code in question: $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?
localzuk Posted July 20, 2009 Author Posted July 20, 2009 How about this? Using the code suggested on the fsockopen page on php.net: /* * * 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: // 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.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now