Friday, December 18, 2009

PHPProxifier. PHP Proxy script.

Intro:

Recently team i'm currently working in received a task to make a proxy script that supports GET, POST, PUT, DELETE HTTP methods. All scripts we've found in network aren't supporting PUT and DELETE methods.
IMPORTANT this script has no fully-featured demo, sample etc. If you are don't know PHP please read PHP manuals before.

Idea:

  • Proxy should be as simple as possible.
  • Proxy should proxy one domain.
Proxy is a script that receives headers, data and pass it to proxied host. Then returns returned headers and data back

Steps of development:

First of all i remember my experience with Snoopy PHP library. It has really good functionality, but old code and code-style, and as we're have agile project with PHPUnderControl that checks it, we've decided that rewriting Snoopy library will consume a lot of time. Anyway i've rewritten it :) and soon it will be available.

Our project based on CodeIgniter has no such things also, but i remembered i saw REST service in Zend Framework and research/work begins.

First of all we don't want to use all of Zend Framework and i stripped out only classes we need:
  • Zend_Controller_Request*
  • Zend_Http_Client*
  • Zend_Http_Cookie
  • Zend_Http_CookieJar
  • Zend_Http_Response
  • Zend_Uri*
  • Zend_Validate_Hostname*
  • Zend_Validate_Ip

Second step was writing a proxy script. You need have php-cURL installed to use this script from scratch. All you need to run it in example mode: download from it's GIT repository

git@github.com:biozshock/PHPProxifier.git

if you are have what to proxy through this script please change $proxyingUrl variable. To enter in debug mode that shows headers, server variables, etc. you need to change $debug variable.

Third thing: to demonstrate feature is working i write script client.php to check all things are working nice. Also this script shows how to implement a page that will call proxy script. To use it you need to change $uri variable to point script to server.php script.
As i don't want to write fully-featured page with all HTTP methods as selector i've just commented out methods you are need to call to get responce.

Conclusion

If you are familiar with PHP and do not know Zend Framework you will easily read code and find out it's simple. If no - please go and read other PHP manuals please.