Building A Simple API Proxy Server with PHP
Join the DZone community and get the full member experience.
Join For Free
the idea is:
... $proxy->register('github', 'https://api.github.com'); ...
and when i type:
http://localhost/github/users/gonzalo123
and create a proxy to :
https://api.github.com/users/gonzalo123
the request method is also important. if we create a post request to localhost we want a post request to github too.
this time we’re not going to reinvent the wheel, so we will use symfony componets so we will use composer to start our project:
we create a conposer.json file with the dependencies:
{ "require": { "symfony/class-loader":"dev-master", "symfony/http-foundation":"dev-master" } }
now
php composer.phar install
and we can start coding. the script will look like this:
register('github', 'https://api.github.com'); $proxy->run(); foreach($proxy->getheaders() as $header) { header($header); } echo $proxy->getcontent();
as we can see we can register as many servers as we want. in this
example we only register github. the application only has two classes:
restproxy
, who extracts the information from the request object and calls to the real server through
curlwrapper
.
<?php namespace restproxy; class restproxy { private $request; private $curl; private $map; private $content; private $headers; public function __construct(\symfony\component\httpfoundation\request $request, curlwrapper $curl) { $this->request = $request; $this->curl = $curl; } public function register($name, $url) { $this->map[$name] = $url; } public function run() { foreach ($this->map as $name => $mapurl) { return $this->dispatch($name, $mapurl); } } private function dispatch($name, $mapurl) { $url = $this->request->getpathinfo(); if (strpos($url, $name) == 1) { $url = $mapurl . str_replace("/{$name}", null, $url); $querystring = $this->request->getquerystring(); switch ($this->request->getmethod()) { case 'get': $this->content = $this->curl->doget($url, $querystring); break; case 'post': $this->content = $this->curl->dopost($url, $querystring); break; case 'delete': $this->content = $this->curl->dodelete($url, $querystring); break; case 'put': $this->content = $this->curl->doput($url, $querystring); break; } $this->headers = $this->curl->getheaders(); } } public function getheaders() { return $this->headers; } public function getcontent() { return $this->content; } }
the restproxy receive two instances in the constructor via dependency injection (curlwrapper and request). this architecture helps a lot in the tests , because we can mock both instances. very helpfully when building restproxy.
the restproxy is registerd within packaist so we can install it using composer installer:
first install componser
curl -s https://getcomposer.org/installer | php
and create a new project:
php composer.phar create-project gonzalo123/rest-proxy proxy
if we are using php5.4 (if not, what are you waiting for?) we can run the build-in server
cd proxy php -s localhost:8888 -t www/
now we only need to open a web browser and type:
http://localhost:8888/github/users/gonzalo123
the library is very minimal (it’s enough for my experiment) and it does’t allow authorization.
of course full code is available in github .
Opinions expressed by DZone contributors are their own.
Comments