Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a real proxy written in PHP or, what would I have to do to implement one?
    primarykey
    data
    text
    <p>I have been looking around on the web for a PHP proxy script and I can't find any. All I keep finding are scripts that download pages and edit <code>src</code> and <code>href</code> attributes in the HTML and, require me to browse to whatever website they're installed on. I don't need this, I need a real proxy listening on a port that I can set my browser to route all it's requests to.</p> <p>What I'm looking to do is:</p> <ol> <li>run a PHP script that will listen on some port of my own computer</li> <li>configure my web browser to use localhost:port as a proxy</li> <li>have the PHP script serve the page to my browser</li> </ol> <p>All the so called proxy scripts written in PHP, that I have found, will fail miserably if the page requested uses <code>XMLHttpRequest</code> to fetch content because they won't see that request at all. No amount of link rewriting will solve the issue short of injecting some clever JavaScript that is unnecessary with a real proxy.</p> <p>What I want in the end, is to have access to the requests and responses going back and forth between my browser and some remote server. I want to be able to use my browser's support for JavaScript, cookies, flash, etc. Basically, I want to have programmatic access to all the communication so I can analyze or manipulate it using PHP.</p> <p>So, the questions are</p> <ul> <li>Have I missed the real proxy implemented in PHP because of all the noise in my search results?</li> <li>If there isn't a real proxy in PHP I would like to try making one. Are there resources online to help me learn what exactly I should do? (note, I RTM religiously: looking more for caveats and architecture of a basic proxy)</li> </ul> <p>Links are appreciated. I know there are several MITM proxies out there but I want one in PHP.</p> <p>I don't know if maybe I could do something with <a href="http://php.net/manual/en/features.commandline.webserver.php" rel="nofollow noreferrer">PHP's built-in webserver</a> but I'll mess with that as well.</p> <p><strong>UPDATE</strong></p> <p>I've got a router script for the PHP built in webserver that is beginning to show promise. I can fire up the webserver and tell my web browser to use it as a proxy. The router script I've made differentiates between local and external resources and gives an easy way of handling either case. The only problem I have is with https. The server reports <code>Invalid Request (Malformed HTTP Request)</code>. I think that means this server won't do https at all with just scripts and settings. I don't know. Maybe I'll be able to do it with Apache but transparently proxying https sounds hard, especially if I want to alter the data before it hits my browser.</p> <p><strong>AtropaToolbox/php_router/router.php</strong> The router script my PHP built in webserver is pointed at, pulls in the actual classes from other files.</p> <pre><code>&lt;?php require_once('AtropaToolbox/php_proxy/proxy.php'); $proxy = new atropa_proxy(); if($proxy-&gt;process_request() === false) { return false; } ?&gt; </code></pre> <p><strong>AtropaToolbox/php_proxy/proxy.php</strong> Extends atropa_mod_proxy to redefine the handlers.</p> <pre><code>&lt;?php require_once('AtropaToolbox/php_proxy/mod_proxy.php'); class atropa_proxy extends atropa_mod_proxy { protected function local_resource_handler() { return false; } protected function external_resource_handler() { $ext = $this-&gt;get_page(); //echo '&lt;pre&gt;' . print_r($ext, true) . '&lt;/pre&gt;'; //$ext['page'] = preg_replace('/&lt;a /', '&lt;p ', $ext['page']); $this-&gt;show_page($ext); } } ?&gt; </code></pre> <p><strong>AtropaToolbox/php_proxy/mod_proxy.php</strong> The generic router script</p> <pre><code>&lt;?php /** * Rev. 1 Atropa mod_proxy for php built in webserver */ class atropa_mod_proxy { protected function is_external_resource() { $host = parse_url($_SERVER['REQUEST_URI'], PHP_URL_HOST); if(isset($host) &amp;&amp; $host !== $_SERVER['SERVER_NAME']) { return true; } else { return false; } } protected function local_resource_handler() { return false; } protected function external_resource_handler() { $ext = $this-&gt;get_page(); $this-&gt;show_page($ext); } public function process_request() { if($this-&gt;is_external_resource()) { return $this-&gt;external_resource_handler(); } else { return $this-&gt;local_resource_handler(); } } public function get_request_headers() { $arr = array(); foreach($_SERVER as $svar =&gt; $sval) { if(substr($svar, 0, 4) === 'HTTP') { $svar = substr($svar, 5); $svar = preg_replace('/_/', ' ', $svar); $svar = ucwords(strtolower($svar)); $svar = preg_replace('/ /', '-', $svar); $arr[$svar] = $sval; } } return $arr; } public function pack_request_headers($headers_array) { $packed = ''; foreach($headers_array as $header_name =&gt; $header_value) { $packed .= $header_name . ': ' . $header_value . "\r\n"; } return $packed; } public function echo_response_headers($http_response_header_array) { foreach($http_response_header_array as $val) { if(strpos(strtolower($val), 'connection') !== 0) { header($val); } } } protected function get_page() { $request_headers = $this-&gt;get_request_headers(); $request_headers = $this-&gt;pack_request_headers($request_headers); $method = $_SERVER["REQUEST_METHOD"]; $scheme = parse_url($_SERVER['REQUEST_URI'], PHP_URL_SCHEME); $opts = array( $scheme =&gt; array( 'method' =&gt; $method, 'header' =&gt; $request_headers ) ); if(count($_POST) &gt; 0) { $content = http_build_query($_POST); $opts[$scheme]['content'] = $content; } $context = stream_context_create($opts); $ext = array(); $ext['page'] = file_get_contents($_SERVER['REQUEST_URI'], false, $context); $ext['http_response_header'] = $http_response_header; return $ext; } protected function show_page($ext) { header_remove(); $this-&gt;echo_response_headers($ext['http_response_header']); echo $ext['page']; } } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload