02-03-2010, 12:05 AM
|
#6 (permalink)
|
|
The Addict
Join Date: May 2009
Posts: 287
Thanks: 5
|
For starters I would recommend creating a db of static content, then have one page accept all of the requests. That page should take in at least a method parameter. You would then want to query the db to grab the results and then parse them out.
Code:
MySQL Tables
Users:
id | username | last_login | location | friends
0 | adam | 2010-02-02 | USA | 42
1 | evan | 2010-02-01 | USA | 57
Posts:
post_id | by | title | content
0 | 0 | Hello, World! | This is a sample post...
1 | 0 | About me. | I'm a fungi!
2 | 1 | About me. | I love to help out.
PHP Code:
// api.php // Import some sort of mysql framework if you want. // Don't forget to sanitize these! $file_type = $_GET['format']; $method = $_GET['method'];
// For starters we could assign a bunch of variables here // to get all of the possible parameters. $username = $_GET['username']; $location = $_GET['location']; $friends = $_GET['friends']; $post_id = $_GET['postid']; $author = $_GET['author']; $title = $_GET['title']; $content = $_GET['content'];
// Now we will want to do specific actions and grab // only what is requested. switch ($method) { case 'posts': // Ok, because the method is a 'posts' we can assume // that they want all of the posts from a user. // E.g. return an array of (post_id, title, content) // for all posts done by $author.
break;
case 'profile':
// This is a profile request, so we would return these // values. // E.g. Return (id, username, location, friends) for $username
break; }
This should give you a good grasp on what an API is, all you're really doing is returning data in a 'computer readable format' (like XML or JSON). Once you have the basic understanding down you can move to a more advanced system like what Evan mentioned.
|
|
|
|