2

I'm a C++/Java guy trying to get started with developing a web service. I've read a LOT about RESTful services and I understand the architecture and how URIs should be formed, and I think I understand the client-side stuff since that is what of what I've found addresses. My question is probably a lot more basic - if I have a URI like this:

http://www.mysite.com/products/gadgets/spypen

what do I do on the server side to set this up? In my (obviously minimal) web programming experience, I would have a directory under HTTP root called "products" and then a subdir under that called "gadgets", and then a subdir "spypen" with an index.html in it. Obviously, this is NOT the way to do it in this type of situation.

It seems to me that I'd like to tell my (Linux-based Apache) web server to interpret the URI that it received and then call a specific PHP script (or whatever) with parameters of "gadgets" and "spypen". I may just be looking in the wrong places, but I can't find any resources that tell me what to do on the server side to get the PHP script called with those params when the URI is received by Apache. Any help would be appreciated and I hope I'm not asking a completely stupid question, but I'm pretty sure I am.. :-) Thanks!

daroo
  • 1,896
  • 4
  • 17
  • 22

1 Answers1

0

Recess is a PHP framework for working with HTTP requests directly. In the java world, you should look at the comments and answer to this SO question for options. Looking at those frameworks will at least give you an idea of what's involved before you try to manually roll your own implementation.

Community
  • 1
  • 1
Sixto Saez
  • 12,610
  • 5
  • 43
  • 51
  • Recess might be interesting after a brief glance.. I had seem some references to it, but it wasn't clear that was its use. Is there a way to do without requiring some off the shelf framework? Maybe I'm thinking about this too much. Will WADL let me do this? That was my first thought, but most comments about WADL are pretty negative out there. – daroo Mar 27 '12 at 19:09
  • Documenting a static "service contract" is usually the reason WADL is criticized in the context of HTTP based applications. If you really leverage the HTTP spec, the role of the "service contract" is played by the media type definition. Using WADL becomes a simpler rehash of the soap mechanisms. A good HTTP app should be written in a more dynamic, self-descriptive way. There are major "arguments" raging around how to design these REST compliant applications. If you're pragmatic, pick & choose what works for you while understanding why you're ignoring the accepted "best practices". – Sixto Saez Mar 27 '12 at 19:26
  • So, looking further, I don't think this is what I'm really after. My system will be GET only, so a client can get data from the server. What I'd really like is for a URI to be received at server, and depending on the URI, have one of N PHP functions called. I did this earlier with SOAP and a WSDL file, and it worked great, but I keep hearing REST is easier, but I'm not sure how to mimic that same behavior in a RESTful way. – daroo Mar 29 '12 at 14:34
  • 1
    An element that is part of the REST architectural style and I think what you're looking for, is [hypermedia linking.](http://www.infoq.com/articles/mark-baker-hypermedia) This means each response from your service contains "links" to the permissible actions for that response. You control the links sent to the client and its logic is coded to follow your links in the response. In the soap world all these actions have to be pre-defined in the WSDL as soap actions. In the REST world, the links are the actions to control the client logic and be created dynamically. – Sixto Saez Mar 29 '12 at 15:51
  • Accepting your answer due to its usefulness - still not really quite was I was after. Instead I ended up just parsing the URI within the php script, which worked for me, but may not be the accepted way of doing this. So I changed the URL from the question to something like http://www.mysite.com/products.php/gadgets/spypen, then in products.php I did some string parsing to extract "gadgets" and "spypen" from $_SERVER["REQUEST_URI"], then used those provided values to query the database appropriately and form the response to the request.. – daroo Apr 04 '12 at 12:46