Page 1 of 1

Where can I find examples of code that uses php and REST ?

Posted: Wed Oct 24, 2007 12:03 pm
by cpomp
I'm new to php and I have a couple of questions. I need to implement this and I don't know what direction to take.
I'm implementing a REST application that for example, the insert's method query string looks like this: "?format=xml&businessID=1234&customerId54321&itemID=123"

My question is,
Where/how do I get the values for businessId and customerId?
How do I invoke the script that will handle the logic for this service?

Can anyone point me in the right direction? Thanks.

Posted: Wed Oct 24, 2007 1:02 pm
by reecec
Hi,

You would use REQUEST or GET

Code: Select all

$business=$_REQUEST['businessId'];
$customer=$_REQUEST['customerId'];
You would now have the value of businessId set in $business and the same for customerID

Hope this helps

Reece

What about in the web page

Posted: Wed Oct 24, 2007 2:05 pm
by cpomp
Thanks.
But what I'm most confused about is how you set that query string on the web page and send it. Any examples of the html code for that?
What event triggers my service to be called and how/where do I get the values for bussinesId and customerId inside the web page so I can put them in the query string before calling the service.

Posted: Wed Oct 24, 2007 2:37 pm
by Toshiba23
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


It's a simple HTML link

Code: Select all

<a href="page.php??format=xml&businessID=1234&customerId=54321&itemID=123">Link</a>
Now in the file this is linking to (page.php), you want to retrieve the GET variables posted to it, like this:

page.php:

Code: Select all

<?

$format = $_GET['format'];
$businessID = (int) $_GET['businessID']; // use (int) to prevent attacks in mySQL, if this variable has anything other than a integer, it will become 0 (zero)
$customerID = (int) $_GET['customerID']; // I don't suggest using this in an address bar but it's your script
$itemID = (int) $_GET['itemID'];

?>
Now you have all your variables in page.php from the address query.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Oct 24, 2007 6:22 pm
by Kieran Huggins
If you're using a REST interface, creating a record should be done using the PUT method (or POST, since browsers are behind the ball supporting PUT and DELETE form methods). Rails differentiates with a hidden _method variable, so that may be a good standard way of working wround the browser limitation.

AJAX calls can be made via PUT and DELETE as well as GET and POST, so I would definitely support those first and foremost, using the alternative syntax as a workaround.

Posted: Wed Oct 24, 2007 9:22 pm
by Christopher
REST is one of those concepts that has come to have so many variations of meaning that it really no long means one thing. However peel away all the various theories and gobbledygook, at its core REST really means that state is transmitted as part of the Request of a resource.

Posted: Wed Oct 24, 2007 11:32 pm
by Kieran Huggins
REST is simply an interface for applying CRUD operations to data models, as far as I can tell. Some people are convinced that you're using REST to retrieve forms, etc... but I disagree. Using some of the principles behind REST when designing your routes is definitely good practice, but I wouldn't limit all your web calls to REST actions only. That's just bizarre IMO.

REST is all about the URI defining the resource and the METHOD defining the action. I dig that. Nested resources are where it gets coooool, because all of a sudden you have context! That's wicked.