Simple Machines Community Forum

Customizing SMF => Bridges and Integrations => Topic started by: Andre N on November 10, 2011, 09:29:31 PM

Title: SMF 2.0 "REST" API
Post by: Andre N on November 10, 2011, 09:29:31 PM
I made this "REST" API for SMF that will work with SMF 2.x that might be helpful to someone.
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 will also 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 folder, 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 folder.

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

Upload the file SmfRestClient.php somewhere, it doesn't need to be on the same domain as the api server folder.

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.

:)
Title: Re: SMF 2.0 "REST" API
Post by: Robert. on November 11, 2011, 10:44:33 AM
Nice work and thanks for sharing :D
Title: Re: SMF 2.0 "REST" API
Post by: zakaluka on November 29, 2011, 01:56:58 PM
Very nice!  Thanks a lot of creating this.

Regards,

z.
Title: Re: SMF 2.0 "REST" API
Post by: orjank on December 08, 2011, 03:58:11 PM
Hi, and thanks for creating this.

I've been trying to integrate smf 2.0.1 with my own site for a while now, and jfusion just isn't cutting it in my case - especially as I am gunning for a frameless integration. My setup is as follows:

SMF is located as follows on my site: www. mydomain. com/sitename/smf/
I put the client file at : www. mydomain. com/sitename/smf/SmfRestClient.php
Your api folder is at : www. mydomain. com/sitename/smf

The client file points to the api folder with the API_SERVER define, and my own site attempts the following:

require_once( './smf/SmfRestClient.php' );
$secretKey = 'SomeKindaSecretKeyGoesHere';
$api = new SmfRestClient( $secretKey );
$success = $api->login_user( 'someusername' );
$result = $api->get_userInfo();


The secret key is the same between the code above and the one specified in SmfRestServer.php, but my problem is that both api calls fail with

Unknown method sitename_smf_api_login_user was called

and

Unknown method sitename_smf_api_get_userInfo was called

... respectively

Any idea what I am doing wrong? Thanks in advance.. :p


Best regards,
Orjan
Title: Re: SMF 2.0 "REST" API
Post by: Andre N on December 08, 2011, 04:44:56 PM
That error message is being thrown by the SmfRestServer.php file because for some reason it's not parsing the request url correctly in the getRoute() function of that file.
the code

protected function getRoute()
    {
        $cwd   = getcwd();
        $cwd   = str_replace($_SERVER['DOCUMENT_ROOT'], '', $cwd);
        $route = str_replace($cwd, '', $_SERVER['REQUEST_URI']);
       
        if ('/' == substr($route, 0, 1)) {
            $route = substr($route, 1);
        }
       
        $this->route = $route;

        return $this;
    }


is getting 'sitename/smf/api/get/userInfo' as the request string instead of 'get/userInfo' which is what it should be in order to create the correct function name.

It looks like getcwd() is giving the wrong current directory, in which case you could set $cwd manually in that function to be '/sitename/smf/api' (or use chdir())

Try that and see if it helps :)
Title: Re: SMF 2.0 "REST" API
Post by: ankifreeze on December 20, 2011, 09:37:21 AM
hi I want to integrate mysite with smf forum in order member in mysite don't need to login twice ... but I got problem.....I using this...require_once('SmfRestClient.php');

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

$api->get_userInfo();

$api->authenticate_user('greenhorse', '123456');

but  it doesn't work?
I think after I using it I don't need login in smf forum anymore.....any suggestion? :(
Title: Re: SMF 2.0 "REST" API
Post by: Andre N on December 20, 2011, 10:58:41 AM
Hi,

This login doesn't set the login cookie on the user browser, it set's it in curl's cookiejar, and the login is only going to be valid for the current API session. Theoretically you could use curl to login and set the login cookie like jquery does, but I haven't had time to mess with it.
It also won't log the user out, as it is intended to run from a different domain and cross-domain policy won't let you delete the cookie. Again, maybe this can be done with curl.

If you're on the same domain, use the regular API, which will let you login and logout easily.
http://www.simplemachines.org/community/index.php?topic=453008.0
Otherwise I'd recommend examining jquery's smf login method and duplicating that.
Title: Re: SMF 2.0 "REST" API
Post by: ankifreeze on December 20, 2011, 06:09:22 PM
hi..yes I using forum and mysite in same domain.... I have used your api but it doesn't work.... :(

log out function doesn't work....

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



login function also doesn't work...


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





Title: Re: SMF 2.0 "REST" API
Post by: Andre N on December 20, 2011, 06:22:35 PM
hi, I will reply to your usage of the regular API in it's thread ;)
Title: Re: SMF 2.0 "REST" API
Post by: newbieuser202 on January 12, 2012, 03:17:14 AM
Hi,

I've installed a new copy of SMF (v.2.0.2) and used your "REST" api.  I tried the very basic function which is login below is the code and results I received.


require_once('SmfRestClient.php');

$secretKey = 'sdgefgbdbdvberger4564trgdfgdfvcvv';
$api = new SmfRestClient($secretKey);
$result = $api->login_user('[email protected]');
print_r($result);


And the result when i user print_r is

stdClass Object ( [data] => true [error] => )


Can you point me where I should check for the error since the code does not log in the user to the SMF forum. 

Thank you,

NB
Title: Re: SMF 2.0 "REST" API
Post by: Andre N on January 12, 2012, 01:05:06 PM
Hi,
There was no error; that is the correct output. The user is logged in through the API only, ie. the cookie is set on the server for the API to use for the duration of the session. The cookie isn't set locally which means if you browse to your forum the user won't be logged in because the cookie isn't set locally.

If you try the other functions you'll see the user is logged in according to the API and you'll be able to access their data, edit, and do whatever you like. Because of cross-domain limitations you won't be able to set login cookie (or delete it, logout) for a user.

If you are on the same domain I recommend you use this version of the API which does not have this limitation and is easier to use as it's only one script:
http://www.simplemachines.org/community/index.php?topic=453008.0

If that is not an option, you might be able to trick SMF into setting the cookie (or deleting it) locally by using cURL to send the login information to your SMF login address. That is the method used by JFusion and seems like it would work , however I haven't had any success doing it yet.
The idea would be to take your SMF login location www.yourforum.com/index.php?action=login2
and cURL the post data, username and password to it and the cookie should be set by SMF.
It would be a nice feature to have and I'll play with it when I get a chance, but if you have any success please post back :)
Title: Re: SMF 2.0 "REST" API
Post by: bidulemachin on June 12, 2012, 02:52:27 PM
Hi,

I have noticed there was a little bug (well in my version).
in SmfRestServer, in the getMethod(), the method was including the first slash, so it was trying to call "_" and then the actual method. So "_authenticate_user" for example.

I changed the code to be that to make it work:
       
$method       = str_replace('/', '_', $parts[0]);
        if(substr($method, 0 ,1) == '_')
        $method = substr($method, 1, strlen($method));

        $this->method = $method;
Title: Re: SMF 2.0 "REST" API
Post by: Sunchaser on July 31, 2012, 11:43:31 AM
Hi, i am trying to integrate a game with the SMF forum v 2.0.2 using the REST API.

I think the function getMethod() is broken, as it tries to search for the wrong method.

Example, if i call

$api -> get_user()

the getMethod search for

smf_api_get_user

while it should search (accordingly to the last version of smf_2_api.php for:

smfapi_getUserData

I would like to know if someone has a version of SmfRestServer.php fixed.

Thanks
Title: Re: SMF 2.0 "REST" API
Post by: Sunchaser on August 03, 2012, 10:46:22 AM
up?
Title: Re: SMF 2.0 "REST" API
Post by: garycarr on August 13, 2012, 08:14:05 PM
I am trying to setup this API and I am unclear on how the api talks to the SMF installation. In the instructions above I see no way to tell the Rest api where to find SMF and how to connect to it.
Title: Re: SMF 2.0 "REST" API
Post by: Kindred on August 14, 2012, 09:16:40 AM
The API file called settings.php...
Title: Re: SMF 2.0 "REST" API
Post by: garycarr on August 14, 2012, 11:53:53 AM
I have create a test script to test connectivity using the API


require_once('SmfRestClient.php');
$secretKey = 'mykey';
$api = new SmfRestClient($secretKey);


$result = $api->check_ifOnline('username');
print_r($result);


I have enabled API_DEBUG in the SmfRestClient.php and the following url gets returned.

www.domain.com/forums/api/check/ifOnline.json?secretKey=mykey&identifier=username


I am not getting any data returned from the API.

I have manually set the path the Settings.php

// specify the settings path here if it's not in smf root and you want to speed things up
$settings_path = $_SERVER['DOCUMENT_ROOT'] . /path/to/forums/Settings.php



Can anyone point to in the direction of what I might be missing?


Thank You in advance!
Title: Re: SMF 2.0 "REST" API
Post by: Thorney on December 04, 2012, 05:04:47 PM
It's all Greek to me! I'll show my co-admin this, he's much better at the coding stuff than I am!
Title: Re: SMF 2.0 "REST" API
Post by: hebelehubele on February 14, 2013, 06:19:28 AM
I use this api for login outside to SMF forum.
require_once('forum/SmfRestClient.php');
$secretKey = 'sdgefgbdbdvberger4564trgdfgdfvcvv';
$api = new SmfRestClient($secretKey);
$api->login_user("master");
$result = $api->get_userInfo();
var_dump($result);


But result is return NULL...

I want one login form which I was created that login users into my site also SMF forum.
Title: Re: SMF 2.0 "REST" API
Post by: Kindred on February 14, 2013, 07:24:49 AM
ummm...   If I read it correctly, you are doing the exact opposite. You are attempting to get the SMF user data, not set it...
Title: Re: SMF 2.0 "REST" API
Post by: hebelehubele on February 14, 2013, 09:02:09 AM
Quote from: Kindred on February 14, 2013, 07:24:49 AM
ummm...   If I read it correctly, you are doing the exact opposite. You are attempting to get the SMF user data, not set it...

Actually, I attempted to get user info that understand user was logged in... Can you help me for this api ? How can I send data to login action ? The data stored in SMF member table in DB
Title: Re: SMF 2.0 "REST" API
Post by: Kindred on February 14, 2013, 09:35:32 AM
wait... you seem to be talking at cross purposes here.

HWich is th eprimary system?

Do you have an outside system which you want to pass data TO SMF and control the interface?
or
Do you have an outside system which you want to accept data FROM SMF and allow SMF to control the interface?
Title: Re: SMF 2.0 "REST" API
Post by: hebelehubele on February 14, 2013, 10:04:22 AM
First one.

I have an outside system. If I login into outside system, login to SMF too.
Title: Re: SMF 2.0 "REST" API
Post by: hebelehubele on February 15, 2013, 03:47:52 AM
Ok I separeted my logins. I send data to smf with iFrame and it login itself. After I register my own session. But SMF login system F.ck my own session.  >:(

How to change smf Session.Save_Path... I think , if I change this I can separate my sessions.
Title: Re: SMF 2.0 "REST" API
Post by: hebelehubele on February 15, 2013, 04:45:51 AM
How to logout from SMF with outside link ?
Title: Re: SMF 2.0 "REST" API
Post by: Arantor on February 15, 2013, 07:41:11 AM
Why not just have *one* login system and be done with it? Much lighter on the system, uses less bandwidth too.
Title: Re: SMF 2.0 "REST" API
Post by: hebelehubele on February 15, 2013, 07:50:08 AM
Because , SMF is later integrate to our CRM software.
Title: Re: SMF 2.0 "REST" API
Post by: Kindred on February 15, 2013, 09:13:46 AM
what does that have to do with anything?

This is what I was trying to get at...   Arantor just put it better.   This entire design should be setting *ONE* system to handle control and then just pass through data.
Title: Re: SMF 2.0 "REST" API
Post by: hebelehubele on February 16, 2013, 02:46:02 AM
I already use one login system... Send login data to SMF login in the backend. I passed this issue.

My actual issue is logout from SMF from backend. I don't kill SMF session with session_destroy();
Title: Re: SMF 2.0 "REST" API
Post by: leggendario on February 20, 2013, 12:31:32 PM
Quote from: Andre N on December 20, 2011, 10:58:41 AM

If you're on the same domain, use the regular API, which will let you login and logout easily.
http://www.simplemachines.org/community/index.php?topic=453008.0
Otherwise I'd recommend examining jquery's smf login method and duplicating that.

I need to login in my test.backend.local and in my test.forum.local
I googled a bit 'around but I could not find anything about this jquery's smf login method
could you give me some info, please?
Title: Re: SMF 2.0 "REST" API
Post by: webgab on March 13, 2013, 07:01:51 AM
Hey there,

I'm new to SMF and this API. I'm developing a browsergame in PHP and want to connect SMF with my game. The game is on my local server (game.domain.com) and SMF is on Webhoster (smf.domain.com).
I have one Question.

The login function:
What does 'Will only log them in remotely as cookies cannot be set cross-domain' mean exactly? In gerneral I'm using the same domain, with differnt sub-domains. Can I set cookies when I'm on the same domain (, but differnt sub-domains)?
What does remotely mean? Will the User be redirected to the Forum and then logged in?

Webgab
Title: Re: SMF 2.0 "REST" API
Post by: Kindred on March 13, 2013, 07:22:33 AM
set your smf cookies to be subdomain independent.
set your other program's cookies at the root domain rather than the subdomain.
Title: Re: SMF 2.0 "REST" API
Post by: webgab on March 29, 2013, 06:59:35 AM
Today I wanted to test this out, but I'm doning something horrible wrong  ;D

I uploaded the api and session folder to the smf root. Edited the secret key etc. And then I made a new php data:

<?
require_once('lib/SmfRestClient.php');
$secretKey = 'Secret Key here';
$api = new SmfRestClient($secretKey, 'raw');
$api->login_user('webgab');
$result = $api->get_userInfo();
var_dump($result);
?>


I'm always getting a 404 error:

string '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /api/get/userInfo.raw was not found on this server.</p>
</body></html>
' (length=218)


Edit: Got the problem: .htaccess wasn't active :D

But now, the login isn't working. I set the cookies to be independant. When I'm pasting the requested URL into my browser anything works fine. But when i'm calling my API script (sub.domain.com/api.php) it doesnt work. Why?
Do I have to put the api.php on the main domain? (domain.com/api.php)

Title: Re: SMF 2.0 "REST" API
Post by: Taidaishar on May 16, 2013, 02:06:58 PM
Quote from: Arantor on February 15, 2013, 07:41:11 AM
Why not just have *one* login system and be done with it? Much lighter on the system, uses less bandwidth too.

I have been combing the treads here and I think this is the what I have been looking for.
I would like to have one registration and log on for my website and forum.
my current file structure on my host is:

public (containing index.php [wich is the welcome/login page])
    Home Directory which includes the multiple webpages as well as Forum directory
           Forum directory (the actual SMF forum)

What I would like to do is ensure the webpages in the Home file are not accessible without loggin in and be able to have the members register and/or log on from the welcome page of my website  (opening the home page of my website upon login) using the SMF login/registration system. Then, through the menu bars on each webpage, be able to navigate to the forums and back without an additional log in prompts.

Is this possible and if so how would I set this up or where can I find the information?
Please let me know if more info is needed.
Title: Re: SMF 2.0 "REST" API
Post by: Kindred on May 16, 2013, 04:33:31 PM
you don't need the API for that at all.
The API is for joining together two disparate systems.

For what you want to do (use SMF login and regsitration and use smf permissions) then SSI is the best choice (and the simplest)
Title: Re: SMF 2.0 "REST" API
Post by: vart001 on August 06, 2013, 03:12:25 PM
I can use the API for this?:
forum: www.domain1.com
website at: www.domain2.com

API: domain2.com need that can do login, restrict access to pages for members only, register etc..

Both domains are on the same server and can share the same database.
You can use the API for this purpose?

Thank you.
Title: Re: SMF 2.0 "REST" API
Post by: Kindred on August 06, 2013, 03:33:19 PM
no.

Logins will NOT transfer across domains... for very good reasons. the login sets a session and a cookie. If the cookie was set for a different domain, it would trigger all sorts of virus warnings in modern browsers.
Title: Re: SMF 2.0 "REST" API
Post by: vart001 on August 06, 2013, 05:01:20 PM
thank you very much.
I understand that the transfer of cookies is not possible for security reasons.

I want 2 websites using the same database and same user system, I think I'll have to abandon my project.
thanks
Title: Re: SMF 2.0 "REST" API
Post by: Kindred on August 06, 2013, 05:09:57 PM
if they are on the same server, you can use feline's subforum mod...
Title: Re: SMF 2.0 "REST" API
Post by: seekaprice on September 18, 2013, 01:51:14 PM
Does any one know where I can get the api files?
Title: Re: SMF 2.0 "REST" API
Post by: Kindred on September 18, 2013, 02:19:29 PM
Did you bother to read the first post?  (the one with the attachments at the bottom?)

Also, did you consider if you need the REST version or the "normal" version?
Title: Re: SMF 2.0 "REST" API
Post by: chinmay235 on October 01, 2013, 10:33:24 AM
Hi i am using

require_once('forum/SmfRestClient.php');
$secretKey = 'sdgefgbdbdvberger4564trgdfgdfvcvv';
$api = new SmfRestClient($secretKey);
$api->login_user("master");
$result = $api->get_userInfo();
var_dump($result);


Please tell me where i get my secrateKey?
Title: Re: SMF 2.0 "REST" API
Post by: Kindred on October 01, 2013, 11:14:21 AM
you make it up
Title: Re: SMF 2.0 "REST" API
Post by: Makavilli on October 02, 2013, 09:29:20 AM
Hello guys could please help me I have downloaded both files and put them where its required but i don't understand what it's meant by: Now you are ready to call the API. You do this by first including the file SmfRestClient.php ....
where should I insert the codes please?? which files??
please I don't know PHP language!  :-\ :-\ :-\
Title: Re: SMF 2.0 "REST" API
Post by: Kindred on October 02, 2013, 10:00:25 AM
Makavilli,
No offense, but if you don't know what you are doing with php, then you really have no business using an API that requires you to know how to code...
Title: Re: SMF 2.0 "REST" API
Post by: Makavilli on October 02, 2013, 10:18:30 AM
I am Learning Man that's why am here ! My Question is only where to put the codes shown above! simple non :)
Title: Re: SMF 2.0 "REST" API
Post by: Sunchaser on October 02, 2013, 10:27:13 AM
Quote from: Sunchaser on July 31, 2012, 11:43:31 AM
Hi, i am trying to integrate a game with the SMF forum v 2.0.2 using the REST API.

I think the function getMethod() is broken, as it tries to search for the wrong method.

Example, if i call

$api -> get_user()

the getMethod search for

smf_api_get_user

while it should search (accordingly to the last version of smf_2_api.php for:

smfapi_getUserData

I would like to know if someone has a version of SmfRestServer.php fixed.

Thanks

Hi, some time ago i posted this bug. Has a new version of the REST api been released and i need to try to replicate the bug with the new version or there is a place when i can submit this bug and it will be fixed?

thanks.



Title: Re: SMF 2.0 "REST" API
Post by: Kindred on October 02, 2013, 10:39:36 AM
Sunchaser,
Since the author has not been active since July 24 - no... there has been no update to the existing API.

Makavilli,
Again, no offense meant, but APIs like this are not really intended for people who are trying to learn.  By all means, try things out, but there are much better ways to learn php coding and to become familiar with it than by jumping into a script whose only purpose is to connect other scripts.
and when you ask a question, you need to be clear.  You say "...where to put the codes shown above..."  What codes? Sown above, where? there are a fair number of responses in this thread already and it's not clear what you are referring to.

Additionally, this is for the REST version of the API.  If you don't know what REST is, there is a good chance that you should not be using this version, but should be using the standard version of the API instead.
Title: Re: SMF 2.0 "REST" API
Post by: Makavilli on October 02, 2013, 10:46:23 AM
what i don't unders is where to insert the following codes


require_once('SmfRestClient.php');

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

$api->login_user('andre');

$result = $api->get_userInfo();


And thank you.
Title: Re: SMF 2.0 "REST" API
Post by: Kindred on October 02, 2013, 10:51:09 AM
well, you would probably never put them in a block like that...

The first post pretty clearly explains where they need to go  --- in different places withing the script that is calling the API functions.

this is what I mean about knowing what you are doing.   Andre's description and instructions assumes that you alreyad know how php scripts are formed and know where you would need to add the links to the API functions in your bridge or other script.


and again...
Quote from: Kindred on October 02, 2013, 10:39:36 AM
Additionally, this is for the REST version of the API.  If you don't know what REST is, there is a good chance that you should not be using this version, but should be using the standard version of the API instead.
Title: Re: SMF 2.0 "REST" API
Post by: Sunchaser on October 02, 2013, 11:28:43 AM
Quote from: Kindred on October 02, 2013, 10:39:36 AM
Sunchaser,
Since the author has not been active since July 24 - no... there has been no update to the existing API.

Makavilli,
Again, no offense meant, but APIs like this are not really intended for people who are trying to learn.  By all means, try things out, but there are much better ways to learn php coding and to become familiar with it than by jumping into a script whose only purpose is to connect other scripts.
and when you ask a question, you need to be clear.  You say "...where to put the codes shown above..."  What codes? Sown above, where? there are a fair number of responses in this thread already and it's not clear what you are referring to.

Additionally, this is for the REST version of the API.  If you don't know what REST is, there is a good chance that you should not be using this version, but should be using the standard version of the API instead.

Can this API then be added to a respository so we can branch and fix things?
Title: Re: SMF 2.0 "REST" API
Post by: Arantor on October 02, 2013, 11:35:25 AM
That's going to raise an interesting dilemma. Should the author do it or should SMF's dev team take it over and maintain it? The problem is that we're a little short on resources to be expanding what we already support, especially as to be honest I haven't even looked at the API yet :/
Title: Re: SMF 2.0 "REST" API
Post by: Sunchaser on October 02, 2013, 11:37:38 AM
I have a webgame that i need to integrate with a smf forum. Either i make the rest api working or i switch to another forum platform, and i would prefer to make the REST api working.

I think at least the bugs should be fixed (provided that i discovered a bug...)...

Title: Re: SMF 2.0 "REST" API
Post by: Arantor on October 02, 2013, 11:39:25 AM
That's fine, and publishing the fixes here would be a great start. That doesn't change anything I pointed out though; whose responsibility is it to maintain the API?

We didn't originate it and right now we don't have the resources to maintain it on top of trying to get SMF 2.1 out and plan for SMF 3.0...
Title: Re: SMF 2.0 "REST" API
Post by: Sunchaser on October 02, 2013, 11:42:10 AM
I think a rest API is strategic for your platform, you should take care of it and mantain it (bug fixing). This, if you don't have any other API... Have you an official API to integrate other applications?
Title: Re: SMF 2.0 "REST" API
Post by: Sunchaser on October 02, 2013, 11:42:23 AM
Quote from: Makavilli on October 02, 2013, 10:46:23 AM
what i don't unders is where to insert the following codes


require_once('SmfRestClient.php');

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

$api->login_user('andre');

$result = $api->get_userInfo();


And thank you.

Hello, You should put that block in PHP script/class where you want to use it. You can create a ForumBridge class and then add there write for example a method called login and other wrappers.


For example, I have a webgame and i want that when a uset logins in the game he automatically logs in the forum.
I can create the class ForumBridge and the login method:

class ForumBridge_Model {

const SECURITYKEY = 'rewrwerwerwerwrrwr';

/**
* autentica l' utente nel forum
* @param: char oggetto utente
* @return: none
*/

function login( $user )
{
require_once('application/libraries/vendors/smf/SmfRestClient.php');
$api = new SmfRestClient(self::SECURITYKEY);

$rc = $api->login_user( $user -> username );
//var_dump( $rc ); exit;
$result = $api -> get_userInfo();
//var_dump( $result ); exit;
        return $result;
}

...



Then in another script i can call the wrapper:


<?php 
$rc 
ForumBridge_Model::login ('sunchaser');
?>


Title: Re: SMF 2.0 "REST" API
Post by: Arantor on October 02, 2013, 11:44:15 AM
Hm, you could just call SSI.php which will do the login thing for you... and if you really wanted to go nuts you could build it on top of SMF for a single entirely integrated set up without even having it be outside the system, including having it deal with things like SQL injection for you too ;)

Having a REST API is certainly not the only way to do it, there is a non REST API plus SSI (which isn't quite what you want)
Title: Re: SMF 2.0 "REST" API
Post by: Sunchaser on October 02, 2013, 11:45:49 AM
Quote from: Arantor on October 02, 2013, 11:44:15 AM
Hm, you could just call SSI.php which will do the login thing for you... and if you really wanted to go nuts you could build it on top of SMF for a single entirely integrated set up without even having it be outside the system, including having it deal with things like SQL injection for you too ;)

Having a REST API is certainly not the only way to do it, there is a non REST API plus SSI (which isn't quite what you want)

ehm, where i can find these APIS? i could not find them... i would like to check them.
Title: Re: SMF 2.0 "REST" API
Post by: Arantor on October 02, 2013, 11:50:07 AM
Well... the other API is even in the sticky topics in this board, SSI is bundled with SMF itself and building it as an extension to SMF directly, well there's no 'separate API' for that (fairly obviously)
Title: Re: SMF 2.0 "REST" API
Post by: Kindred on October 02, 2013, 11:53:38 AM
read the wiki?

http://wiki.simplemachines.org/smf/Category:Integrating_SMF
Title: Re: SMF 2.0 "REST" API
Post by: Sunchaser on October 02, 2013, 11:58:10 AM
Quote from: Kindred on October 02, 2013, 11:53:38 AM
read the wiki?

http://wiki.simplemachines.org/smf/Category:Integrating_SMF

Thank you. If i understood well, if for example I have a Forum board Kingdom of London and i want that the board can be accessed ONLY if the player is actually located in London in the game, I need to use the Integration Hooks.
Title: Re: SMF 2.0 "REST" API
Post by: Arantor on October 02, 2013, 12:01:24 PM
That's a much, much more complex task than any use of APIs will be because it's not how SMF is designed to work.
Title: Re: SMF 2.0 "REST" API
Post by: Kindred on October 02, 2013, 12:02:43 PM
when you say "board" do you actually mean board in the SMF sense... e.g. one of the things listed on your board index that holds the message threads?

If so, then you just need to place the user in a membergroup called "london" and give that group access to the board.

If you need to read the user's location real-time and determine their access (add or remove access) based on realtime data, then you are talkign about writing something from scratch that is not currently handled by ANY system in SMF, API, hooks or SSI.
Title: Re: SMF 2.0 "REST" API
Post by: Sunchaser on October 02, 2013, 12:07:23 PM
Quotewhen you say "board" do you actually mean board in the SMF sense... e.g. one of the things listed on your board index that holds the message threads?

Yes.

QuoteIf you need to read the user's location real-time and determine their access (add or remove access) based on realtime data, then you are talkign about writing something from scratch that is not currently handled by ANY system in SMF, API, hooks or SSI

Ok, _I_think_ I can solve it in another way:

1) I use SSI.php to show the London Board content in a page available only in the Kingdom of London;
2) I use the API to let only Kingdom of London citizens post in that board.


Title: Re: SMF 2.0 "REST" API
Post by: Kindred on October 02, 2013, 12:16:47 PM
or you can just collect the post in a form and use the standard SMF function create_post()?
Title: Re: SMF 2.0 "REST" API
Post by: Sunchaser on October 02, 2013, 12:21:58 PM
Quote from: Kindred on October 02, 2013, 12:16:47 PM
or you can just collect the post in a form and use the standard SMF function create_post()?

yes
Title: Re: SMF 2.0 "REST" API
Post by: koma on January 21, 2014, 05:08:23 AM
Is there a possibility to modify post with the api? I tried to search for that, but couldn't. Any change for getting that possibly in hte future?
Title: Re: SMF 2.0 "REST" API
Post by: koma on February 14, 2014, 02:13:13 AM
Hi, I've been trying to implement this rest API, and ran into, trouble. I tried to get the posterdata from get?userinfo *or what it was( function, seems like it doesn't return array (or decent json object) but a string presentation of the array dump. Is this intented?

and I think I found a slight bug in SmfRestServer.php,

on line 375
is:
if (!isset($topicOptions['board']) || !isset($msgOptions['subject']) || !isset($msgOptions['body'])) {

it should be (I believe):
if (!isset($this->topicOptions['board']) || !isset($this->msgOptions['subject']) || !isset($this->msgOptions['body'])) {
Title: Re: SMF 2.0 "REST" API
Post by: Akiracr on March 29, 2014, 12:12:59 AM
Hi,

I tray to implement the REST API solution, my code is as follows:

$api = new SmfRestClient(SMF_INTEGRATION_KEY);
$login = $api->login_user('[email protected]');
$result = $api->get_userInfo();


I test the login and return true but when dump the $result variable return this:


stdClass Object
(
    [data] => stdClass Object
        (
            [groups] => Array
                (
                   
Title: Re: SMF 2.0 "REST" API
Post by: spyphone2015 on April 07, 2014, 11:06:46 PM
Thanks share for me. i also error : Unknown method sitename_smf_api_login_user was called. please help me
Title: Re: SMF 2.0 "REST" API
Post by: matt.ernst on May 06, 2014, 01:50:40 AM
Akiracer, koma, you are both getting the "raw" formatted data instead of JSON data. I had the same problem. I thought maybe it was because I'm using a less-common setup (nginx and php5-fpm). I didn't bother to figure out where the logic was going wrong. I just changed line 186 of SmfRestServer.php to replace 'raw' with 'json'. I'm always going to be using the JSON calls so I didn't care about fixing the detection logic.

I also had to convert the .htaccess rewrite rule to the nginx equivalent, in case anyone else is trying to do the same.

At the end of the server block for your SMF installation, put

location /api {
  rewrite .* /api/index.php last;
}

(of course replacing /api with your actual installation directory, if you have changed it).
Title: Re: SMF 2.0 "REST" API
Post by: xeon365 on November 11, 2014, 09:59:25 AM
Quote from: matt.ernst on May 06, 2014, 01:50:40 AM
Akiracer, koma, you are both getting the "raw" formatted data instead of JSON data. I had the same problem. I thought maybe it was because I'm using a less-common setup (nginx and php5-fpm). I didn't bother to figure out where the logic was going wrong. I just changed line 186 of SmfRestServer.php to replace 'raw' with 'json'. I'm always going to be using the JSON calls so I didn't care about fixing the detection logic.

I also had to convert the .htaccess rewrite rule to the nginx equivalent, in case anyone else is trying to do the same.

At the end of the server block for your SMF installation, put

location /api {
  rewrite .* /api/index.php last;
}

(of course replacing /api with your actual installation directory, if you have changed it).


good fix... what a head bang that was.. thought I had forgotten how to use php arrays, the return was just a string that looked like a array or object.
Title: Re: SMF 2.0 "REST" API
Post by: codeforgood on February 11, 2015, 07:50:44 PM
Quote from: Andre N on January 12, 2012, 01:05:06 PM
If that is not an option, you might be able to trick SMF into setting the cookie (or deleting it) locally by using cURL to send the login information to your SMF login address. That is the method used by JFusion and seems like it would work , however I haven't had any success doing it yet.
The idea would be to take your SMF login location /index.php?action=login2
and cURL the post data, username and password to it and the cookie should be set by SMF.
It would be a nice feature to have and I'll play with it when I get a chance, but if you have any success please post back :)

Interesting idea, have you tested this approach?

I have the same requirement. I'm looking to create a not-for-profit sports club fan app that integrates multiple communication channels into one. This will give our team's fans a central place to stay up to date / discuss their team. One of the source channels is a SMF forum which i don't manage. I'd like to consume the SMF posts via an API that i will develop, hence my initial question.

Any feedback / ideas would be really appreciated. I'm new to SMF.

Thanks
Title: Re: SMF 2.0 "REST" API
Post by: z1haze on July 17, 2015, 03:10:45 PM
I may be grave digging, so excuse me but I am a brand new user to the SMF community. I was just wondering is this REST api still functional, and is it the most current RESTful api available for SMF?
Title: Re: SMF 2.0 "REST" API
Post by: Kindred on July 17, 2015, 03:21:24 PM
well, nothing really has been done with it since it was created... but the backing system has not changed significantly since its creation, either...
Title: Re: SMF 2.0 "REST" API
Post by: z1haze on July 17, 2015, 03:50:03 PM
Quote from: Kindred on July 17, 2015, 03:21:24 PM
well, nothing really has been done with it since it was created... but the backing system has not changed significantly since its creation, either...

Can you help me with using it briefly? I am coming from XenForo which has similar API that I use to register new members via inside a game from a plugin that basically calls http://mysite.com/api.php?action=register&key=mykey&username=username&[email protected] etc etc

With this I can add new users to my database safely and easily. How can I do that with this api? Would I have to basically write my own file that implements this api or can I just do it straight from mysite.com/api/...
Title: Re: SMF 2.0 "REST" API
Post by: Kindred on July 17, 2015, 05:24:06 PM
I believe that you have to rite your own file that calls functions from the API...   but the first post in this thread... and the wiki section on integrating SMF should have more explanation
Title: Re: SMF 2.0 "REST" API
Post by: z1haze on July 17, 2015, 09:23:23 PM
I've not been aple to send the activation email to the user when adding members with the smf_2_api.php
Title: Re: SMF 2.0 "REST" API
Post by: z1haze on July 18, 2015, 11:50:24 AM
If anyone knows how to send the activation email via the SMF api or the REST api please let me know.
Title: Re: SMF 2.0 "REST" API
Post by: bo100nka on December 11, 2015, 12:55:44 PM
hey guys,
when i try to send pm using send pm or show logout link functions and then print_r it i get this strange error:
Try again, the settings path should be saved now.

any idea what could have i done wrong?
Title: Re: SMF 2.0 "REST" API Bug on create post
Post by: wbros on April 20, 2016, 10:39:34 AM
On SmfRestClient.php have a big bug on create post:

protected function create_post()
    {
        try {
            $this->loadSSI();
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }
        global $sourcedir;
        require_once($sourcedir . "/Subs-Post.php");

        $this->msgOptions = unserialize($this->msgOptions);
        $this->topicOptions = unserialize($this->topicOptions);
        $this->posterOptions = unserialize($this->posterOptions);

        if (!isset($topicOptions['board']) || !isset($msgOptions['subject']) || !isset($msgOptions['body'])) {
           $this->data = 'false';
        } else {
            $this->data = createPost($this->msgOptions, $this->topicOptions, $this->posterOptions);
        }
    }
You use $topicOptions, $msgOptions without $this reference. Always is "false".

Please, change this to:

if (!isset($this->topicOptions['board']) || !isset($this->msgOptions['subject']) || !isset($this->msgOptions['body'])) { $this->data = 'false';
Title: Re: SMF 2.0 "REST" API
Post by: andrejpod on June 29, 2016, 04:34:33 AM
Hello,

it would be really nice to present REST api as a connection to arbitrary platform, that is to document the raw REST interface for example to use it in Ror, Java or .Net.
Title: Re: SMF 2.0 "REST" API
Post by: Kindred on July 01, 2016, 06:35:18 PM
Sure.. go ahead and get that started....
Title: Re: SMF 2.0 "REST" API
Post by: andrejpod on July 12, 2016, 08:16:04 AM
Can you please give me a raw example of registering a member through a HTTP post with username, password and email?

So in the form of HTTP get/post on

<smf_forum>/api/register/member?secretkey=foo&user=uname&pass=pwd&[email protected]

is something like this possible?
Title: Re: SMF 2.0 "REST" API
Post by: Geekologist on May 14, 2017, 02:14:03 PM
$result = $api->send_pm(array("to" => array(1)), "test", "test", 0, array(3), 0);

The database value you're trying to insert does not exist: to_members


What am I doing wrong?
Title: Re: SMF 2.0 "REST" API
Post by: Geekologist on June 18, 2017, 12:14:06 PM
Doesn't seem to work with the new 0.14v system, I had it working perfectly with the .13v system.. But now it seems there is some kind of problem with the auth part of system.

It just simple returns the login page on api request, but if I take the request URL and manually run it thru browser it works fine because I'm logged in.

Any idea how to fix?
Title: Re: SMF 2.0 "REST" API
Post by: Arantor on June 18, 2017, 12:37:17 PM
2.0.14 changed how logins work. Not sure how feasible it is to fix.
Title: Re: SMF 2.0 "REST" API
Post by: Geekologist on June 18, 2017, 12:41:45 PM
Quote from: Arantor on June 18, 2017, 12:37:17 PM
2.0.14 changed how logins work. Not sure how feasible it is to fix.

Snap! ..

Oh well, I'll recode my system to do something which doesn't require the API, as I don't there is much chance for the OP to update the API
Title: Re: SMF 2.0 "REST" API
Post by: Geekologist on June 18, 2017, 01:03:08 PM
My external site has access to SMF db so creating a function which does a action in the SMF db isn't a problem.

The only thing I needed the API for was to send an PM to a user, everything else I've been able to code myself, but I do not understand how the PM setup is working, I tried creating a function which sends a PM, but didn't work.. Not sure how it all hangs together.. Anyone able to give me an example maybe?
Title: Re: SMF 2.0 "REST" API
Post by: Arantor on June 18, 2017, 01:24:33 PM
As I said in your other thread, use SMF's function to do it. Plus, it is generally discouraged to talk about the innards of PMs here because people who shouldn't be spying on their users' messages will learn how to. (This has been a rule here for at least 10 years now.)
Title: Re: SMF 2.0 "REST" API
Post by: darren.a.roberts on November 27, 2017, 03:30:47 AM
So I've just upgraded to 2.0.15 and the server API stuff is broken. I call this API from another site (using Java) so it looks like I'm stuffed?

If I read correctly from the thread above there are no plans to continue support going forward?
Title: Re: SMF 2.0 "REST" API
Post by: Kindred on December 02, 2017, 08:22:19 PM
The original author of the api has not been active for a long time...    and the api is not actually official smf code...

So, the api May eventually be updated, but given the fact that the official smf focus is on 2.1 beta/RC/final, it may not be for a long time
Title: Re: SMF 2.0 "REST" API
Post by: Faysal230 on September 15, 2018, 02:54:01 AM
I try to show my from recent post on my main site,
my configuration is

require_once('SmfRestClient.php');
$secretKey = 'ZTUDgM9K4EJVWB4dllbyFS58zmn0QVAA';
$api = new SmfRestClient($secretKey);
$result = $api->show_recentPosts();
var_dump($result);

But not get any result only blank page.
Title: Re: SMF 2.0 "REST" API
Post by: Kindred on September 17, 2018, 01:49:43 PM
As the post above you notes... I don't believe that this API works on 2.0.15
Title: Re: SMF 2.0 "REST" API
Post by: adrianvk on December 29, 2022, 09:44:50 AM
Hi, in SMF 2.1.1 not working.

The value of the cookie that it generates is very different from the one that SMF generates if you do the manual login.

Any solution?

Thank you
Title: Re: SMF 2.0 "REST" API
Post by: Kindred on December 29, 2022, 11:07:15 AM
As the above posts indicate -- this RESTful API has not worked in the 2.0.x branch for a long time -- and it most certainly does not work in the 2.1.x branch
Title: Re: SMF 2.0 "REST" API
Post by: adrianvk on December 30, 2022, 02:41:17 AM
I found the solution in case it helps someone:

In smf_2_api.php file, replace line 860:
smfapi_setLoginCookie(60 * $cookieLength, $user_data['id_member'], sha1($user_data['passwd'] . $user_data['password_salt']));
For this:
smfapi_setLoginCookie(60 * $cookieLength, $user_data['id_member'], hash_salt($user_data['passwd'],$user_data['password_salt']));