TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Api (http://www.talkphp.com/advanced-php-programming/5248-api.html)

Tanax 02-01-2010 09:00 PM

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.

:)

maeltar 02-01-2010 10:12 PM

you are looking for a "content management system"

adamdecaf 02-01-2010 10:28 PM

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.

Village Idiot 02-02-2010 05:12 AM

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.

ETbyrne 02-02-2010 11:00 PM

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.

adamdecaf 02-03-2010 12:05 AM

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.

codefreek 02-03-2010 03:11 PM

This link, should give you some ideas..
http://www.webresourcesdepot.com/how...-10-tutorials/


All times are GMT. The time now is 02:57 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0