TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-01-2010, 09:00 PM   #1 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default Api

How would I construct an API system, kinda like facebook?
Couldn't search for it because it was too short..

I really have no idea how to start so.. yea.

:)
__________________
Tanax is offline  
Reply With Quote
Old 02-01-2010, 10:12 PM   #2 (permalink)
The Acquainted
 
Join Date: Nov 2009
Location: nr Stratford-Upon-Avon
Posts: 137
Thanks: 3
maeltar is on a distinguished road
Default

you are looking for a "content management system"
__________________
Thanks... Simon

Sex, Drugs & Linux Rules
Send a message via MSN to maeltar
maeltar is offline  
Reply With Quote
Old 02-01-2010, 10:28 PM   #3 (permalink)
The Addict
 
Join Date: May 2009
Posts: 287
Thanks: 5
adamdecaf is on a distinguished road
Default

A basic API is really just a web interface (data wise), but you send the data in a RESTful way (xml,json,php). You just need to create a system that will respond to requests and provide the needed data.

If you're still stuck look through some of these examples: Vimeo simple API or Twitter API. Really, a good example of an 'api' is any RSS/XML feed, all they are is data which your RSS reader parses and displays.
__________________
My Site
adamdecaf is offline  
Reply With Quote
Old 02-02-2010, 05:12 AM   #4 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Years of people's lives have been spent answering that question, it can't be done in one post here. As Adam said, look though those examples and begin to piece it together yourself.
__________________

Village Idiot is offline  
Reply With Quote
Old 02-02-2010, 11:00 PM   #5 (permalink)
how quixotic are you?
 
ETbyrne's Avatar
 
Join Date: Dec 2007
Location: Lapeer, MI
Posts: 445
Thanks: 37
ETbyrne is on a distinguished road
Default

If you are already using a MVC framework, like dingo or CI, then implementing a twitter-like api shouldn't be too difficult. For example let's say you have a page URL like so:

Code:
user/view/evan
The data access (or API) URL could then be something like one of these:

Code:
user/view/evan/xml
user/view/evan/json
You would then have to modify your controller method to see if the user is requesting the regular page or a data API page. So, you could take this user controller:

PHP Code:
class controller
{
  public function 
view($name)
  {
    
// Get data and show page
  
}

And change it to something like this:

PHP Code:
class controller
{
  public function 
view($name,$type='html')
  {
    
// Get data

    
if($type === 'html')
    {
      
// Show page
    
}
    
    elseif(
$type === 'xml')
    {
      
// Show XML version of data
    
}

    elseif(
$type === 'json')
    {
      
// Show JSON version of data
    
}

    else
    {
      
$this->load->error('404');
    }
  }

I'm sure you could develop some sort of library, helper, or even framework to make this more streamlined, but that's the basic idea. Another idea would be to use the URL router to make API requests go to an entirely different controller.
__________________
Dingo Web Systems > http://www.dingocode.com
My Website > http://www.evanbot.com
ETbyrne is offline  
Reply With Quote
Old 02-03-2010, 12:05 AM   #6 (permalink)
The Addict
 
Join Date: May 2009
Posts: 287
Thanks: 5
adamdecaf is on a distinguished road
Default

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.
__________________
My Site
adamdecaf is offline  
Reply With Quote
Old 02-03-2010, 03:11 PM   #7 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

This link, should give you some ideas..
http://www.webresourcesdepot.com/how...-10-tutorials/
codefreek is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 07:12 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design