MyBB Community Forums

Full Version: Post a thread from PHP script
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
function postThread ($subject, $body) {
	// Set up posthandler.
	require_once MYBB_ROOT."inc/datahandlers/post.php";
	$posthandler = new PostDataHandler("insert");
	$posthandler->action = "thread";

	// Set the thread data that came from the input to the $thread array.
	$thefid=2; //change this
	$theicon=13;
	$theuid=1; //change this
	$theusername="admin"; //change this
	$new_thread = array(
		"fid" => $thefid,
		"subject" => $subject,
		//"prefix" => $mybb->get_input('threadprefix', MyBB::INPUT_INT),
		"icon" => $theicon,
		"uid" => $theuid,
		"username" => $theusername,
		"message" => $body,
		"ipaddress" => $session->packedip,
		//"posthash" => $mybb->get_input('posthash')
	);

	//if($pid != '')
	//{
	//	$new_thread['pid'] = $pid;
	//}

	// Set up the thread options from the input.
	$new_thread['options'] = array(
		"signature" => 1,
		"subscriptionmethod" => "email",
		"disablesmilies" => 1
	);

	// Apply moderation options if we have them
	$new_thread['modoptions'] = array(
		"stickthread" => 1
	);

	$posthandler->set_data($new_thread);

	// Now let the post handler do all the hard work.
	$valid_thread = $posthandler->validate_thread();

	$post_errors = array();
	// Fetch friendly error messages if this is an invalid thread
	if(!$valid_thread)
	{
		$post_errors = $posthandler->get_friendly_errors();
	}

	// One or more errors returned, fetch error list and throw to newthread page
	if(count($post_errors) > 0)
	{
		$thread_errors = inline_error($post_errors);
		$mybb->input['action'] = "newthread";
	}
	// No errors were found, it is safe to insert the thread.
	else
	{
		$thread_info = $posthandler->insert_thread();
		$tid = $thread_info['tid'];
		$visible = $thread_info['visible'];
	}
}
The script fails with the following errors:

Quote:line 1: syntax error near unexpected token `$subject,'
line 1: `function postThread ($subject, $body) {'

Edit: Adding:

Quote:#!/usr/bin/php
<?php

to the start of the script and:

Quote:?>

to the end allows the script to run but no new thread is created. Can you please tell me if the thread subject and message are supposed to be added to the script array like this?:

Quote:$subject="Subject";
$body="Message";
(2020-12-14, 06:39 AM)msm1 Wrote: [ -> ]The script fails with the following errors:

Quote:line 1: syntax error near unexpected token `$subject,'
line 1: `function postThread ($subject, $body) {'

Edit: Adding:

Quote:#!/usr/bin/php
<?php

to the start of the script and:

Quote:?>

to the end allows the script to run but no new thread is created. Can you please tell me if the thread subject and message are supposed to be added to the script array like this?:

Quote:$subject="Subject";
$body="Message";
Since it's a function, you'll likely want to include this script with php tags in a template (assuming you have the template conditionals plugin), you would need to call it for to run. You could get it to work completely externally by also including like global.php.

You need to set the $message and $body variable yourself.


<?php

     $subject = "insert title here";
     $body    = "insert body here";
     postThread($subject, $body);
?>

You would likely need a little bit of coding knowledge to be able to get it completely working for your situation or setup.
(2020-12-16, 07:13 AM)Poliwag Wrote: [ -> ]Since it's a function, you'll likely want to include this script with php tags in a template (assuming you have the template conditionals plugin), you would need to call it for to run.  You could get it to work completely externally by also including like global.php. 

You need to set the $message and $body variable yourself.


<?php

    $subject = "insert title here";
    $body    = "insert body here";
    postThread($subject, $body);
?>

You would likely need a little bit of coding knowledge to be able to get it completely working for your situation or setup.

Thanks for your response. I should have mentioned in my post that adding "$subject = "Title";" and "$body= "Message body";" didn't work. I tested the script on both a Ubuntu VPS (SSH session) and locally running XAMPP.

Sadly I don't have any. If it's required, maybe the thread should be moved from the tutorial section as it isn't a tutorial.