News:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu

New tools to help integrating SMF with something else

Started by Andre N, September 19, 2011, 01:35:36 PM

Previous topic - Next topic

Andre N

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. Upload it 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:

[code]require_once '/path/to/hooks/file/smf_2_integration_hooks.php'

[/code]

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.

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

edit 11/1/11 updated integration hooks file to make fixes to the save and restore functions

NEW - I made a 'REST' API for SMF that you can download and use. Check out it's thread here:
http://www.simplemachines.org/community/index.php?topic=458832.0
It let's you use all the SSI functions, all the API functions, a couple other things like post and pm, it's easy to extend, and you do it all from a different domain than your SMF install (or same if you like) :)

edit 2/29/12 updated API to version 0.1.2 to fix some bugs and make it work with magic quotes hopefully
"Every generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?"

ghrom

Can I crate a new thread using this? Say, from Objective-C iPhone app?

Andre N

You could, but you'd have to write your own function for that in the API. See Sources/Post.php for the code SMF uses
"Every generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?"

ghrom

Thanks. Is there any chance you will be adding this in your api, please?

EDIT: Actually, it was easier than I thought. Thanks for pointing me in the right direction!

BREZ

I am a bit confused.

Exactly where would the smfapi_logout($email); go in 4images script?

Andre N

Quote from: BREZ on October 30, 2011, 12:50:58 PM
I am a bit confused.

Exactly where would the smfapi_logout($email); go in 4images script?

It would go wherever that script logs the user out. So look in the 4images script for the point where the user gets logged out of 4images and add the API code beneath that.
Make sure that you have included the API script already, and that $email has their email address, SMF username or SMF member ID
"Every generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?"

DanCarroll

Maybe you can help me decide on the direction to take my project. I'm developing a CRM for a client and am looking at basing it on SMF. My dilemma is in what would be the best approach. At this point I like the idea of adding to the SMF system. Save some duplication of effort and code by using the member (user) system and messaging system. I found this thread and would like clarification on the following quote from the first posting in this thread.

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

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


  • Should I set this up as an external system and use the integration hooks to control my app?
  • Or have my app be stand alone and control SMF through the API?
  • Maybe even set it up as a Mod and integrate that way?
  • Or go with my current plan and integrate it into the SMF system via custom actions?

Any and all help on this is greatly appreciated.

Thanks, Dan C.

Andre N

"Every generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?"

DanCarroll

A CRM app (Customer Relationship Management). Basically it is to track clients & customers, events & purchases, and prospects & phone calls. I have the data model figured out and was looking at adding it to the SMF forum and the forum members would be "staff or employees". Would need to integrate it into the Calendar system, etc.

Using PHP & mySQL so shouldn't be a problem. I didn't have a good understanding of the API and integration hooks. What about the SMC functions? Are the SMC functions part of the $smcFunc system?

I have the 2.0.1 version of SMF up and running with both a production and a test system. I just need to start developing the web app part of my CRM system. Basically just need data entry and reporting functions to a database.

Andre N

The SMC functions are
$smcFunc => Array
    (
        [db_query] => smf_db_query
        [db_quote] => smf_db_quote
        [db_fetch_assoc] => mysql_fetch_assoc
        [db_fetch_row] => mysql_fetch_row
        [db_free_result] => mysql_free_result
        [db_insert] => smf_db_insert
        [db_insert_id] => smf_db_insert_id
        [db_num_rows] => mysql_num_rows
        [db_data_seek] => mysql_data_seek
        [db_num_fields] => mysql_num_fields
        [db_escape_string] => addslashes
        [db_unescape_string] => stripslashes
        [db_server_info] => mysql_get_server_info
        [db_affected_rows] => smf_db_affected_rows
        [db_transaction] => smf_db_transaction
        [db_error] => mysql_error
        [db_select_db] => mysql_select_db
        [db_title] =>
        [db_sybase] =>
        [db_case_sensitive] =>
        [db_escape_wildcard_string] => smf_db_escape_wildcard_string
        [entity_fix] =>
        [htmlspecialchars] =>
        [htmltrim] =>
        [strlen] =>
        [strpos] =>
        [substr] =>
        [strtolower] =>
        [strtoupper] =>
        [truncate] =>
        [ucfirst] =>
        [ucwords] =>
    )

They're just the functions mainly used with the SMF db. They are already available if you are in SMF, or if you call the API. including the API script is like loading the "SMF object". It will give you (easier) access to the SMF db and all the settings and variables like $context and $modSettings

So by including the API script wherever SMF hasn't been instantiated you can use those db functions, plus the other API functions, plus you have access to those settings variables. Is that what you're looking for?
"Every generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?"

DanCarroll

Is that what I'm looking for? Maybe.

I guess I just need to know:

  • Are the API functions available inside of SMF to  add functions to the SMF system?
  • And is there a better way to go to add pages (actions) with database access?

Also, thirdly, would I be committing an egregious error in adding my data tables to the SMF database?

Thanks, Dan

Andre N

The API functions are just stripped down versions of a few of the functions SMF has, and they are meant to be used outside of SMF. The API doesn't extend SMF, it lets you use some of it's functions from outside. Inside you have lots more.

Best way would probably be to add your pages as actions, then they would already have db access. You could look at how some of the modification packages do it, I believe you'd need to insert your action into the actions array in index.php, create a file for your action in the sources folder, and create a template for it (your page). If there's a better way I don't know it.

If your table names are different than SMF's you'll be fine :)
"Every generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?"

DanCarroll

Thanks. That is the way I'm proceeding. Already tested setting up actions, sub-actions, linktree support, and page titles. I'll have to take a look at some of the Mod code then. Is there any code examples for newbs to the board (not new to coding)?

Guess  I should be looking into the Mod creation process. What about the SMF Function database? Can I utilize (call) those functions? Or is there an API I should use? Or an SDK?

Just asking if there is a comprehensive list of functions that can be used for extending the SMF system since you stated that "the API functions are just stripped down versions of a few of the functions SMF has"?

Thanks again, hope I can contribute some time, Dan

Andre N

You can call all the functions in the function database; no need for the API inside SMF.

For the complete list of all the SMF functions google 'smf function database', and you'll see the functions available to you with some description.

The DB functions are inside the variable $smcFunc and you'll be able to call them all; no need for the API. You'll have access to everything SMF does. If you look at some of the files in Sources folder you'll see how the smcFunc's are used; pretty easy to understand when you see the code.
"Every generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?"

DanCarroll


ankifreeze

hi I have used your api but it doesn't work.... :(

log out function doesn't work....
Code: [Select]

require_once ( 'smf_2_api.php');
smfapi_logout('[email protected]');


login function also doesn't work...
Code: [Select]


require_once ( 'smf_2_api.php');
smfapi_authenticate('ankifreeze', '123456')

Andre N

Hi,

For login, use

smfapi_login($email);

as authenticate will not log a user in, it's only use is to authenticate a username/password combo

as for logout, it should work flawlessly. If you log in manually and then call the logout function on yourself, does it work?

If you try calling it for random users, it will remove them from the database as being logged, so they won't show as being online anymore, but their cookie will persist so when they actually visit the forum they will still be logged in. If you call logout on the current user, only then will the login cookie be destroyed as well ;)

See if that helps. Otherwise it's possible your cookie settings prevent you from accessing SMF's cookie... let me know :)
"Every generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?"

ankifreeze

greatt..it does work now...but it doesn't work if log in manually and then call the logout function from api....I see there are some different function in smf_2_api.php and smf script forum ...is  it safe  using your api? ..btw nice tutorial and script.....thanks very much.... :)

ankifreeze

#18
I got problem when first time I try log in....Notice: Undefined variable:USER_AGENT in C:\xampp\htdocs\site\forum\smf_2_api.php on line1867 ...I found $_SESSION['USER_AGENT'] is not defined...but after that I try to log in and It works....


and I got problem  when I try logout...I can't logout from mysite ( can't logout if I put logout.php in http://localhost/site/logout.php (mysite folder) ) but it s works if I put  logout.php in http://localhost/site/forum/logout.php (forum folder)

my forum place
http://localhost/site/forum/index.php
my site place
http://localhost/site/index.php

also I  have tried register using API like this...

require_once ( 'smf_2_api.php');
$reg=array();
$reg['member_name']='frank';
$reg['email']='[email protected]';
$reg['require']='nothing';
$reg['password']='123456';
$reg['password_check']='123456';
smfapi_registerMember($reg);


$reg= array(
'member_name' => 'frank',
'email' => '[email protected]',
'require' => 'nothing',
'password' => '123456',
'password_check' => '123456',
);
smfapi_registerMember($reg);


it  works but I feel something wrong and strange....any suggestion... :(

Andre N

Hi,

First error, change:

} elseif (empty($id_member) && isset($_SESSION['login_' . $cookiename]) && ($_SESSION['USER_AGENT'] == $_SERVER['HTTP_USER_AGENT'] || !empty($modSettings['disableCheckUA']))) {


to be


} elseif (empty($id_member) && isset($_SESSION['login_' . $cookiename]) && !empty($modSettings['disableCheckUA'])) {


If they haven't visited the forum this session variable might not be set.

2. If your logout script is working on path /forum but not / then the problem is with your cookie. You need to make sure the cookie path is set for '/' and not for '/forum'. Do this in SMF admin by enabling subdomain independent cookies. Then delete your cookie, login and try it again.

3. You're doing the registration correctly, and it works... what do you mean you feel something wrong and strange? lol :)
"Every generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?"

Advertisement: