Firefox PHP

Integration with external application

Posted by OnDistantShores 
Integration with external application
May 11, 2009 03:11PM
I have been looking round the web for a PHP forum that has an API, and Phorum seemed to be the only one I could find. I need to integrate the forum's data layer into an external application (to be able to add, edit, delete posts, etc, without any requirement for a front-end) so the fact that there was a dedicated DB layer API in Phorum seemed perfect.

However, actually integrating Phorum with my application turned out to be a lot harder than expected. I had assumed that the DB layer would be a standalone component that can be included in any application, but that's not the case. The DB layer (namely '/include/db/mysql.php') is closely coupled with it's intended caller '/common.php'. I've tried hacking around and setting defines such as PHORUM and PHORUM_DEFAULT_LANGUAGE and setting the global $PHORUM['DBCONFIG'], but I am still having issues with include paths.

Has anyone managed to do what I have done, to use the data layer and nothing else? How did you manage all the dependencies and definitions required to hook directly into mysql.php?

My last resort is to copy mysql.php and use it as a base to make my own data layer, but I'm hoping not to have to do that.

Thanks in advance!



Edited 1 time(s). Last edit at 05/11/2009 03:12PM by OnDistantShores.
Re: Integration with external application
May 11, 2009 08:28PM
the best way to have all related stuff loaded is to just load common.php which takes care of the rest.


Thomas Seifert
Phorum Development Team / Mysnip-Solutions.de
Custom Phorum and general software development
worry-free Phorum Hosting
Re: Integration with external application
May 11, 2009 08:44PM
Using the pure database layer might be a little bit too low level (just a guess, I don't know your exact application). I would suggest to stay at the API layer on top of that. For some data, the DB layer is merely a data setter and getter and the logic happens in the API layer. Like Thomas says, including common.php will setup the base requirements. From there on, you can use both API layer code and the DB.

In 5.3, we are working on an idea to have an easier to use API system, especially when calling it from other applications. To give you a short preview, here's how one could call the functions:

Language: PHP
<?php   require_once ';/path/to/your/phorum/include/api.php';; $phorum = Phorum::API();   $phorum->user->save(array( ';user_id'; => 1234, ';username'; => ';testuser';, ';password'; => ';testpass';, ';password_temp'; => ';testpassje';, ';email'; => ';maurice@phorum.org';, ';active'; => PHORUM_USER_ACTIVE ));   $userdata = $phorum->user->get(1234);   ?>

Note:
Although this looks like OO code, it is only the API call router (Phorum::API()) that is an object here. Underneath, this maps to the good ol' functional API calls that we have implemented up to now. The magic in the API call router is that when $phorum->user->save() is called, the API router will:
  • load the API layer "include/api/user.php" if needed (so no need to include include/api/*.php yourself)
  • call phorum_api_user_save()

The difference with loading common.php, is that the code in include/api.php is less large. If the size of common.php is keeping you from loading that file, then this might be something that you like.

Some other changes as opposed to the current method, is that "phorum_page" does no longer need to be defined (the API code will set it to "unknown" if it's not defined already) and that it is no longer needed to do a chdir() to the Phorum code tree.

The database layer is also available under the API router object. For example the function phorum_db_user_get() will be automatically available as $phorum->db->user_get().

Possibly, this is what you were looking for.
Possibly not, but at least we tried ;-)


Maurice Makaay
Phorum Development Team
my blog linkedin profile secret sauce
Re: Integration with external application
May 12, 2009 10:41AM
Hey guys and thanks for the responses - I didn't hold out much hope on hearing back because the forum seemed dead, so I really appreciate your thoughts!

Thomas, I did look at just including common.php, but the same problem exists - I have to define constants such as PHORUM_ADMIN and more importantly, the include paths are in the format './path...' so including them from another location (as is inevitable with an external application) causes errors. Any suggestions on how to get around this (short of editing Phorum's code, of course)?

Maurice, your new API with access to the "db" object sounds perfect, that's exactly what I need to get access to all the data (not just that available with the current "API"). When will this new API be available?

Cheers
Re: Integration with external application
May 12, 2009 10:52AM
Maurice, I suppose the other question is - with the code as it currently stands, is there any way the DB functions can be accessed through the API (i.e. simply by including 'include/api.php')?
Re: Integration with external application
May 12, 2009 10:53AM
The usual solution is simply ...

Language: PHP
$cwd = getcwd() chdir(';/your/phorum/directory';); include ';./common.php';; chdir($cwd);

defining one or two constants shouldn't be too bad.


Thomas Seifert
Phorum Development Team / Mysnip-Solutions.de
Custom Phorum and general software development
worry-free Phorum Hosting
Re: Integration with external application
May 12, 2009 10:57AM
In the current work code from trunk, the DB layer is automatically loaded from api.php and an API router is instantiated for it. In code:

Language: PHP
include(';/path/to/api.php';); $phorum = Phorum::API(); $user = $phorum->db->user_get(1234);

That will effectively call the db layer function phorum_db_user_get(1234).


Maurice Makaay
Phorum Development Team
my blog linkedin profile secret sauce
Re: Integration with external application
May 12, 2009 11:13AM
Cheers Maurice, I'll start working with the trunk version. But how long are we looking at before the release of 5.3?

Also, how does database connection work with this new API? Is there an interface for setting connectivity details?
Re: Integration with external application
May 12, 2009 04:01PM
Nothing changed really. The way in which you set configuration parameters is the same as before. So include/db/config.php is loaded. There is no specific interface for changing these settings, but you could of course directly change the PHP data structure that holds the settings. Another way is to set the global variable $PHORUM_ALT_DBCONFIG to override the config from include/db/config.php.

How long it will be before 5.3 is ready is hard to tell really. It depends on how much time we can spend on it and when we think it is ready for release. There's a lot of work in implementing API layer code and changing the rest of Phorum to use that API code. Additionally, some new features might be implemented. I know that it would be nice to retrieve an estimate, but I honestly cannot provide you one.


Maurice Makaay
Phorum Development Team
my blog linkedin profile secret sauce
Sorry, only registered users may post in this forum.

Click here to login