Skip to content

Instantly share code, notes, and snippets.

@therealklanni
Created November 22, 2011 04:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save therealklanni/1384925 to your computer and use it in GitHub Desktop.
Save therealklanni/1384925 to your computer and use it in GitHub Desktop.
Stupidly simple PHP proxy
<?php
// Stupidly simple PHP proxy for AJAX (HTTP GET) requests. Written by Kevin Lanni.
$dest = ''; // Set to the remote script URL (i.e. http://remotehost.com/some.php)
$a = array();
foreach ($_GET as $k=>$v) {
$a[] = "{$k}={$v}";
}
echo file_get_contents($dest.'?'.implode('&',$a));
?>
@adius
Copy link

adius commented Apr 6, 2012

Oh man. I just had a really hard time because of your proxy. I was looking for the bug everywhere in my Code until I finally remembered that I use your proxy. ...and there it was!
I like the proxy because it's "stupidly simple" and that's sometimes all you need.
However, I think you made it to simple.

The $_GET variable is urldecoded and you're using this encoded Version for the query. You should, however, use the encoded Version of the parameter because otherwise it's not possible to use your proxy for encoded urls. :-/

This will fix it:

foreach ($_GET as $k=>$v) {
    $a[] = urlencode($k).'='.urlencode($v);
}

@adius
Copy link

adius commented Apr 6, 2012

....or even simpler:

<?php
$dest = '';
echo file_get_contents($dest.'?'.http_build_query($_GET));
?>

;-)

@adius
Copy link

adius commented Apr 6, 2012

...

<?php
$dest = '';
echo file_get_contents($dest.'?'.$_SERVER['QUERY_STRING']);
?>

@therealklanni
Copy link
Author

Thanks, those are some pretty good ideas. Feel free to fork this gist :)

I'll try your methods out when I have a little time to play around and then update my gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment