REST - PHP Extension
| From: | Ioseb Dzmanashvili | Date: | Mon, 03 Jan 2011 11:08:48 +0000 |
| Subject: | REST - PHP Extension | ||
| Groups: | php.pecl.dev | ||
| Request: | Send a blank email to pecl-dev+get-7904@lists.php.net to get a copy of this message | ||
Dear All,
I'm Ioseb Dzmanashvili, software engineer from Republic of Georgia. Recently
I begun working on quite huge corporate project, one of the main purposes of
this project is to build RESTful API layer in front of each system for
simplifying and improving interoperability between various systems. As was
decided I begun implementing REST client and server as a separate PHP
extension. I got permission from my boss to open source this project and as
was recommended on pecl.php.net I want to introduce this extension.
Extension consist of two parts:
> Client - for easily and effectively building clients for any RESTful
resource; and
> Server - for building RESTful resources.
API for both parts is pretty straightforward. Below are presented some usage
examples of RestServer, in case of interest I'll send examples of RestClient
as well. You can see the source of the current implementation at
https://github.com/ioseb/rest , please note that
it's in a very early stage
of development and is far from polished and finished product.
Thank you in advance!
*RestServer example:*
$server = new RestServer(
'/test/filter/index.php' //service endpoint
);
$server->addRoute(array(
/**
* path template, will be compiled to:
*
~^/news/?(?P<hola>(?:/[-\w]+)+)/(?P<test>(?:/?[^/]*))/(?P<path>pppp)/1/(?P<token>tttt)/?(?P<date>(?:/\d+)+)$~
*/
'#path' => '/news/{hola}/{test}/{path}/1/{token}/{date}',
'#tokens' => array(
'token' => 'tttt', //match tttt
'path' => 'pppp', //match pppp
'hola' => REST_ROUTE_PATTERN_ALPHA_TOKENS, //match /abc/efd/123 type
alphanumeric sub path
'date' => REST_ROUTE_PATTERN_DIGIT_TOKENS, //match /12/34/56/78 type
numeric sub path
),
'#get' => 'getResponse', //callback for HTTP GET
method
))->addRoute(array(
/**
* path template, will be compiled to:
* ~^/hello/?(?P<hola>(?:/.+)+)$~
*/
'#path' => '/news/{hola}',
'#tokens' => array(
'hola' => REST_ROUTE_PATTERN_TOKENS, //match /abc/efd/123 type
sub path, any characters allowed
),
'#get' => 'getResponse', //callback for HTTP GET
method
));
try {
$result = $server->handleQueryParam('q'); //try to match path provided as
a query parameter e.g. ?q=/path/to/some/resource
//$result = $server->handleRequestUri(); //try to match path provided
as a request URI e.g.
http://domain.com/service-endpoint/path/to/some/resource
//$result = $server->handle('/path/to/some/resource'); //try to match
path provided as a regular method parameter
} catch (Exception $ex) {
printf('<pre>%s</pre>', print_r($ex, TRUE));
}
function getResponse($args) {
print_r($args);
}
*Printed result by "getResponse($args)" callback for:
http://localhost/test/filter/index.php?q=/news/a1/b2/g3/d4/e5*
Array
(
[hola] => Array
(
[0] => a1
[1] => b2
[2] => g3
[3] => d4
[4] => e5
)
)
*Printed result by "getResponse($args)" callback for** for:
http://localhost/test/filter/index.php?q=/news/a1/b2/g3/d4/e5/kjjhll/pppp/1/tttt/10/20/30*
Array
(
[hola] => Array
(
[0] => a1
[1] => b2
[2] => g3
[3] => d4
[4] => e5
)
[test] => kjjhll
[path] => pppp
[token] => tttt
[date] => Array
(
[0] => 10
[1] => 20
[2] => 30
)
)
*Dump of $server object:*
RestServer Object
(
[endpoint] => /test/filter/index.php
[routes] => Array
(
[0] => Array
(
[#path] => /news/{hola}/{test}/{path}/1/{token}/{date}
[#tokens] => Array
(
[token] => tttt
[path] => pppp
[hola] => (?:/[-\w]+)+
[date] => (?:/\d+)+
)
[#get] => getResponse
[#expr] =>
~^/news/?(?P<hola>(?:/[-\w]+)+)/(?P<test>(?:/?[^/]*))/(?P<path>pppp)/1/(?P<token>tttt)/?(?P<date>(?:/\d+)+)$~
)
[1] => Array
(
[#path] => /news/{hola}
[#tokens] => Array
(
[hola] => (?:/.+)+
)
[#get] => getResponse
[#expr] => ~^/news/?(?P<hola>(?:/.+)+)$~
)
)
)
Best regards,
Ioseb
--
Ioseb Dzmanashvili
Senior Software Architect
Picktek LLC
41 Vazha-Pshavela ave.
Tbilisi, 0177, Georgia
T (+995 32) 39.58.56, F (+1 202) 315.3934
picktek.com
github.com/ioseb
twitter.com/iosebi