JeremyW
JeremyW

Reputation: 743

RESTful in PHP : getting the encoding parameter in a url

I'm building a RESTful API in PHP. I'd like to let the user display any result in 3 different encodings (json, xml & rss).

Here is an example of a GET request for the user 123456873:

 - GET /user/123456873.json -> Json
 - GET /user/123456873.xml -> xml
 - GET /user/123456873.rss -> rss

How could I get the encoding parameter (.json/.xml/.rss) included in the URL with PHP?

Upvotes: 0

Views: 1487

Answers (3)

JeremyW
JeremyW

Reputation: 743

Here is the solution:

URL rewriting via mod_rewrite in your .htaccess :

RewriteEngine on
RewriteRule ^(.*)$ api.php?handler=$1 [L,QSA]

/user/123487364.json -> api.php?handler=user/123487364.json

In api.php :

$request = $_GET['handler']
$encoding = explode(".", $request); -> to get the encoding (array)
$parameters = explode("/", $encoding[0]); -> to get the parameters (array)

Upvotes: 0

baloo
baloo

Reputation: 7755

The solution is different on different servers but in general you need to configure it to serve PHP even if the path is not ending with .php
This way the PHP application can parse the path and decide what to do with it.

For Apache this can be accomplished by the rewrite module. Google told me you should read this guide: http://www.logon2.com.au/blog/archive/web-design/php-apache-mod-rewrite-tutorial/

Upvotes: 0

Langusten Gustel
Langusten Gustel

Reputation: 11002

You are looking for rewrite engine. Continue reading here: http://en.wikipedia.org/wiki/Rewrite_engine

for Apache this could be interesting: http://net.tutsplus.com/tutorials/other/a-deeper-look-at-mod_rewrite-for-apache/

Upvotes: 1

Related Questions