0

I have various sections on my website, such as a blog, forum, wiki etc and I'd like to develop an API to encompass all these sections, and allow users to pull information from any section.

Users should also be able to pull information (such as latest posts) from multiple sections at once and return the results. The API should be very modular, so as to allow new sections to be added easily if, for example, another blog is added.

I'm just looking for a point in the right direction for developing something like this.

Thanks.

4 Answers 4

0

For content sites, an actual API is rarely necessary, unless you want to allow users and clients to post to your site.

What you actually need to do is just to separate logic from presentation.

Per example:

$articles = $pdo->query('SELECT * FROM articles')->fetchAll();

if (isset($_GET['type'])) {
    switch ($_GET['type']) {
        case 'json':
            header('Content-Type: application/json');
            echo json_encode($articles);
            break;
        case 'xml':
            header('Content-Type: text/xml');
            // some array to XML function
            break;
    }
} else {
    include 'my_html_template.phtml';
}

Of course, this is a very basic example. But once you've separated your presentation from your logic, you can easily create templates in a multitude of different formats, which can be parsed using different tools (JSON encoder, XML parser, etc.) by different clients (mobile application, aggregator service, third-party application, etc.).

1
  • Thanks. I hadn't thought about it doing it this way. Jun 13, 2011 at 0:35
0

I think you should begin with exploring MVC design pattern in the PHP world, along the way you will find answers to many of your questions...

0
0

It sounds like you're just trying to create a web service in PHP.

The "sections" of the website aren't terribly important, because the service doesn't need to be structured identically to the site itself. Think of it like a separate site sitting on top of the same database, smaller and without a UI.

For example, if you create a SOAP service then you'd probably just expose a function or two (or three, etc.) for each section of the site, accepting parameters to specify the data to be returned. Date ranges for blog posts, user IDs for forum posts, search terms for Wiki articles, etc.

0

There are some formats you can user to serve your data: JSON, XML, etc.

You would have to have different url's to retrieve the data, eg:

/blog/{id} -> gets a blogpost

/blog/{id}/comments -> gets a blogpost comments

/blog/add -> creates a blogpost

etc

for all your actions (and sections)

You also might want some way of authentication before users are allowed to use your API.

Just some things you can think about.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.