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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cpomp
Forum Newbie
Posts: 4
Joined: Tue Oct 23, 2007 8:07 pm

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

Post 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.
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post 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
cpomp
Forum Newbie
Posts: 4
Joined: Tue Oct 23, 2007 8:07 pm

What about in the web page

Post 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.
Toshiba23
Forum Newbie
Posts: 4
Joined: Wed Oct 24, 2007 12:39 pm

Post 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]
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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.
Post Reply