forumsmfsmf-forumsimple-machines-forum

Post to SMF Forum via PHP


I am building a CMS that supports a website which also contains an SMF forum (2.0.11). One of the modules in the CMS involves a "report" that tracks attendance. That data is queried to tables outside of smf, but in the same database. In addition to what it does now, I would also like for a post to be made in a specific board on the SMF forum containing the formatted content. As all of the posts are contained by the database, surely this is possible, but it seems there is more to it than a single row in a table.

To put it in the simplest code possible, below is what I want to happen when I click Submit on my page.

$title = "2015-03-04 - Attendance";
$author = "KGrimes";
$body = "Attendance was good.";

$SQL = "INSERT INTO smf_some_table (title, author, body) VALUES ($title, $author, $body)";
$result = mysqli_query($db_handle, $SQL);

Having dug through the smf DB tables and the post() and post2() functions, it seems there is more than one table involved when a post is made. Has anyone outlined this before?

I've looked into solutions such as the Custom Form Mod, but these forms and templates are not what I am looking for. I already have the data inputted and POST'ed to variables, I just need the right table(s) to INSERT it into so that it appears on the forum.

Thank you in advance for any help!


Solution

  • Source: http://www.simplemachines.org/community/index.php?topic=542521.0

    Code where you want to create post:

                        //Define variables
                        //msgOptions
                        $smf_subject = "Test Title";
    
                        //Handle & escape
                        $smf_subject = htmlspecialchars($smf_subject);
                        $smf_subject = quote_smart($smf_subject, $db_handle);
    
                        $smf_body = "This is a test.";
    
                        //Handle & escape
                        $smf_body = htmlspecialchars($smf_body);
                        $smf_body = quote_smart($smf_body, $db_handle);
    
                        //topicOptions
                        $smf_board = 54; //Any board id, found in URL
    
                        //posterOptions
                        $smf_id = 6; //any id, this is found as ID in memberlist
    
                        //SMF Post function
                        require_once('../../forums/SSI.php');
                        require_once('../../forums/Sources/Subs-Post.php');
    
                        //createPost($msgOptions, $topicOptions, $posterOptions);
                        // Create a post, either as new topic (id_topic = 0) or in an existing one.
                        // The input parameters of this function assume:
                        // - Strings have been escaped.
                        // - Integers have been cast to integer.
                        // - Mandatory parameters are set.
    
                        // Collect all parameters for the creation or modification of a post.
                        $msgOptions = array(
                            'subject' => $smf_subject,
                            'body' => $smf_body,
                            //'smileys_enabled' => !isset($_POST['ns']),
                        );
                        $topicOptions = array(
                            //'id' => empty($topic) ? 0 : $topic,
                            'board' => $smf_board,
                            'mark_as_read' => true,
                        );
                        $posterOptions = array(
                            'id' => $smf_id,
                        );
    
                        //Execute SMF post
                        createPost($msgOptions, $topicOptions, $posterOptions);
    

    This will create the simplest of posts with title and body defined by you, along with location and who the author is. More parameters can be found in the SMF functions database for createPost. The SSI and Subs-Post.php includes must be the original directly, copying them over doesn't do the trick.