ajax-proxy: A PHP AJAX Proxy with Cookie Support

A common annoyance in the world of AJAX is the ol’ cross-domain request error. That is, a script sitting on domain1.example.com can’t make AJAX calls to domain2.example.com. This is in place for the sake of security, but sometimes developers have legitimate reasons to do so.

A common workaround is writing a server-side script that will sit on domain1.example.com, and make proxy requests to domain2.example.com, and send the output back to the client. Many times, this is a “quick and dirty” solution to a common problem. You can find many of these short ‘n ugly procedural proxy scripts online.

But what if our needs are more complicated? What if our AJAX calls needed to be made over an authenticated session which requires forwarding of cookies? This is much harder to find online, and is a bit more complicated.

Lastly, just try and find an AJAX proxy that doesn’t rely entirely on cURL as it’s request mechanism. Developers using shared hosting or servers with draconian admins might not of the luxury of access to third-party fluff like cURL.

Over the past few weeks, I’ve developed ajax-proxy over at HUGE in Brooklyn, which is a class and script that solves both of the problems above. Additionally, it’s functionality is entirely encapsulated inside of a re-usable class. It was made so that it could stand alone on a server as proxy.php, or be incorporated into a larger framework like Zend, Kohana, or CodeIgniter.

Here are it’s features:

  • Uses cURL, but falls back to the less-speedy fopen if the former sin’t available
  • Forwards cookies and other headers in both directions
  • Has optional error and exception handling for behavior as a standalone script (frameworks would likely not want this)
  • Extremely well documented (I’m a documentation fanatic)
  • Written entirely in OO PHP 5, to the Zend standards

Additionally, the proxy underwent 2 weeks of development and intensive code review by the HUGE development team before it’s v1.0 push to GitHub. This is by no means a “quick and dirty” solution — it’s a rock-solid, full featured ajax-proxy that’s dead easy to use. Assuming proxy requests were going to be made to a base url of domain2.example.com, the code to run the proxy looks like:

$proxy = new AjaxProxy('domain2.example.com');
$proxy->execute();

If there’s anyone that loves simplicity, it’s me. Of course, there are more constructor options, but that’s better left to the README.

Anyway, the project is posted on GitHub at: http://github.com/hugeinc/ajax-proxy, where there is also pretty extensive documentation on the class’ usage. For documentation about the internal class members and methods, everything is doc’d with PHPDoc.

As a side note, I think it’s about time that I add KLogger to github. I get a fair amount of emails from people telling me they’re using it, and it’s about time I incorporated some new features in, like rolling log files, etc. I’m tired of having dusty old zip files sitting around. I’ll make another post when I put it up there!

This entry was posted in Uncategorized. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.
  • Pingback: ajax-proxy.php; easily send cross-domain ajax requests via PHP | Sankho Mallik . com

  • MJ

    Hi, I’m trying to use this together with 404 error handlers to port a part of heavy ajax html form to my server (for the sake of letting it work on my server, while posting the results to the original server) –
    I’ve got couple comments – it doesn’t work on all servers because of getallheaders compatibility – you might want to use that function (found somewhere on the web) to handle that:

    function emu_getallheaders() {
    foreach($_SERVER as $h=>$v)
    if(ereg(‘HTTP_(.+)’,$h,$hp))
    $headers[$hp[1]]=$v;
    return $headers;
    }

    That would have been a cool concept… unfortunately it doesn’t work as expected, since the browser get all too serious about 404 error and are failing to work with 404’d documents :/

  • http://scottbush.net Scott Bush

    Kenny, this proxy sounds like *exactly* what I need for my current project; I’m so glad you’re sharing it. But I have yet to get it to work. I’ve tried a number of configurations but all I ever get is:

    Fatal proxy Exception: ‘A valid response was not received from the host’ in /var/www/vhosts/[path]/[to]/[site]/ajaxProxy.php:505

    The same result appears whether the proxy is accessed directly through the browser (a number of them, actually) or through code, multiple data sources (including delicious’s feeds in XML, JSON and RSS formats), with or without the two optional parameters to AjaxProxy constructor, on a true webserver or a local webserver, with or without web proxies configured for my computer. I really want this to work, but haven’t been able to find an example to follow anywhere.

    Do you have any thoughts on this, or can you post a simple example that works?

  • Pingback: Lesson learned: You can’t access DOM elements within an external iFrame | Vidal Quevedo

  • Gihan

    I just found this great article when I’m in the exact situation. But I cannot find the github project of your proxy. Could you please give me(correct here) the correct url. Thank you

  • katzgrau
  • Gihan

    Great stuffs!, Thanks

  • Michael Straubinger

    Hi! My server is returning two session cookies. They are returned to the client from th php-proxy but are completely empty and I get a 401 unauthorized error. With direct access everything works fine. I am running Apache btw.