SMF API

From Online Manual

Jump to: navigation, search

There are two API scripts available for SMF 2.0.x. Both created by Andre, one is for REST systems, the other is not.

"Regular" API for SMF 2.0.x

You want to integrate SMF with something else. Here's 2 files that will help you accomplish that.

The API will let you control SMF from your other system.

The integration hooks will let you control your other system from SMF

How to use the API:

1. Download the two attachments here. Upload them to your server. Anywhere. Make sure your SMF installation is on the same server. It will seek out your installation and create a settings.txt file with the location of your Settings.php file for it's own future reference (to make it load faster)

2. In your script, include the API file. Include it anywhere you like as long as it gets included before you try to use it's functions.

require_once '/path/to/api/file/smf_2_api.php'

3. Call the functions. They are documented pretty well but if you have questions ask below. Example:

smfapi_logout($email);

Would log the user out of SMF. This makes the assumption that you have the users email address in the string $email. You would put this code in your other system just before or after the user is logged out there, so now they are logged out together.

4. Repeat for all the other functions you need to use.


How to use the integration hooks:

1. Make it easy on yourself and download the integration hooks skeleton file here. It has been written for maximum ease of use.

2. Upload the skeleton file onto your server. Anywhere. Make sure your SMF installation is on the same server.

3. Open the file index.php in your SMF root directory. This is SMF's 'main' index.php file. At the very top, include the hooks file:

require_once '/path/to/hooks/file/smf_2_integration_hooks.php'

4. Write the code inside the functions that will manipulate your other system. Taking logout as an example, inside function smf_logout_function($memberName) you might put:


function smf_logout_function($memberName)
{
$sessionData = smf_session_save_function(); //save and close the SMF session

require_once('other/system/file.php');
other_system_function_logout($memberName);

smf_session_restore_function($sessionData); //load the SMF session and put session data back
}

5. Repeat this process for all the hooks you want to use.

  • Hint: For the hooks you can get SMF variables in the functions by declaring them as global.

function smf_logout_function($memberName)
{
global $user_info, $context;
//now you have the user info and context variables SMF has available to your function
}

  • Hint: For the hooks, if you aren't sure what variables are being passed to it, do something like this:

function smf_logout_function($memberName)
{
var_dump($memberName);
exit();
}

Then log out of SMF and you will see what information SMF is passing to the hook.

  • edit 11/1/11 updated integration hooks file to make fixes to the save and restore functions
  • edit 2/29/12 updated API to version 0.1.2 to fix some bugs and make it work with magic quotes hopefully

Download the attachments and get support Questions, comments and suggestions welcome. Please post your finished hook files for other systems too and it will help others using that system to integrate


"REST" API for SMF 2.0.x

Andre made this "REST" API for SMF that will work with SMF 2.0.x. The reason REST is in quotes is it's not a true RESTful API but it will allow you to use the API and SSI functions on your SMF installation from a different domain (it can also be used in the same domain). It will let you create posts, send pm's and it is easy to extend to do other things...

How to use: Download the two attachments here, the client and server. Extract the server package and upload it somewhere on your SMF forum domain.

In the api directory, open the SmfRestServer.php file and on line 47 specify your SECRET_KEY. Make it secure!! A long string that nobody will guess. Save it and upload it to the api directory.

Open the file SmfRestClient.php and on line 52, set the path to your api directory, the one you just uploaded.

Upload the file SmfRestClient.php somewhere, it does not need to be on the same domain as the api server directory.

Now you are ready to call the API. You do this by first including the file SmfRestClient.php :

require_once('SmfRestClient.php');

then you will instantiate the SmfRestClient object like this:


$secretKey = 'sdgefgbdbdvberger4564trgdfgdfvcvv';
$api = new SmfRestClient($secretKey);

Be sure you set the $secretKey variable to be your unique secret key that you set above.

Now you can use the API functions by calling them like this:

$api->login_user('andre');

You can get the results/output of your API call like this:

$result = $api->get_userInfo();

the results will be a stdClass Object by default.

If you instantiate the API client with the parameter 'raw' like this:

$api = new SmfRestClient($secretKey, 'raw');

you will get the output as an array instead.

download the REST API attachments and get support