OnpubAPI Introduction

Every download of Onpub includes OnpubAPI (Onpub Application Programming Interface). OnpubAPI is a collection of PHP 5 objects and functions that enable full programmatic control of Onpub-created content.

OnpubAPI gives PHP programmers an easy way to manipulate and display Onpub-created content. This makes it possible to create custom website frontends, while still using Onpub to manage the content, organization, and backend of the site.

Onpub also includes a default frontend layout that uses OnpubAPI to display Onpub-created content. This code can be used as a starting point for creating custom Onpub-managed website layouts and designs.

Onpub stores all the content you create with it in a MySQL database. A MySQL database with the Onpub schema (i.e., database tables) installed is referred to as an Onpub Database from here on.

OnpubAPI uses PDO for connecting to and querying an Onpub Database. This tutorial shows you how to successfully connect to your Onpub Database in order to use OnpubAPI to access and display the content you created with Onpub.

Before you can use OnpubAPI to access your data, you must first connect to MySQL by instantiating a PDO object:

<?php

// Establish the connection to the Onpub database.
$pdo = new PDO"mysql:host=localhost;dbname=test""test""test" );

?> 

The syntax for the above constructor is documented here.

For host=localhost, replace the localhost part with the actual hostname of your MySQL server. Leave this as localhost if MySQL is running on the same machine as your web server.

For dbname=test, replace the test part with the actual name of the MySQL database where the Onpub tables are installed. This would be the same as what you enter in to the Database field when you login to Onpub.

For the first "test" argument, replace test with the username you use to login to MySQL. This would be the as what you enter in to the Username field when you login to Onpub.

For the second "test" argument, replace test with the password you use to login to MySQL. This would be the same as what you enter in to the Password field when you login to Onpub.

The PDO constructor will throw an exception if there's an error. If there's no error, the $pdo object now represents a connection to the Onpub Database. You can now use this connection in concert with OnpubAPI to query content you created with Onpub.

For example, this is how you would use the $pdo object created above to display how many articles are in the Onpub Database:

<?php

// Include the OnpubAPI classes.
include 'onpub/api/onpubapi.php';

// Construct an OnpubArticles object. This represents the OnpubArticles table
// in the database.
$oarticles = new OnpubArticles($pdo);

// Output the number of articles currently stored in the database.
echo $oarticles->count();

?>  

The first line of the above code includes OnpubAPI. This is required so that PHP knows where to find the commands used on the next two lines. Adjust the path to onpubapi.php if Onpub is installed in a different directory on your machine.

The second line of the above code constructs an OnpubArticles object. The OnpubArticles object defines several function/methods that allow you to quickly query the OnpubArticles table in the database.

The third line of code calls the OnpubArticles count method which returns the number of articles that are currently stored in the database.

This concludes the tutorial on how to connect to and query an Onpub Database. As you can see it's fairly straighforward to access the data you create with Onpub from your own PHP scripts. OnpubAPI gives you full access to your data without requiring you to write complex SQL queries.