Customizing SMF > Portals, Bridges, and Integrations

SMF 2.0 "REST" API

(1/15) > >>

Andre N:
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 :

--- Code: ---require_once('SmfRestClient.php');

--- End code ---

then you will instantiate the SmfRestClient object like this:

--- Code: ---$secretKey = 'sdgefgbdbdvberger4564trgdfgdfvcvv';
$api = new SmfRestClient($secretKey);

--- End code ---
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:

--- Code: ---$api->login_user('andre');

--- End code ---

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

--- Code: ---$result = $api->get_userInfo();

--- End code ---
the results will be a stdClass Object by default.

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

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

--- End code ---
you will get the output as an array instead.

:)

Kryzen:
Nice work and thanks for sharing :D

zakaluka:
Very nice!  Thanks a lot of creating this.

Regards,

z.

orjank:
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:


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

--- End code ---

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


--- Code: ---Unknown method sitename_smf_api_login_user was called
--- End code ---

and


--- Code: ---Unknown method sitename_smf_api_get_userInfo was called
--- End code ---

... respectively

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


Best regards,
Orjan

Andre N:
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

--- 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;
    }

--- End code ---

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 :)

Navigation

[0] Message Index

[#] Next page

Go to full version