Thread: Api
View Single Post
Old 02-02-2010, 11:00 PM   #5 (permalink)
ETbyrne
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