Post a thread from PHP script

Spartak

Well-known member
Member
Joined
9 yrs. 9 mth. 5 days
Messages
587
Reaction score
8,878
Wallet
0$
Post a thread from PHP script

<pre data-deferred="true" class="block-code line-numbers language-none"><code class="language-php">function postThread ($subject, $body) {
// Set up posthandler.
require_once MYBB_ROOT.&quot;inc/datahandlers/post.php&quot;;
$posthandler = new PostDataHandler(&quot;insert&quot;);
$posthandler-&gt;action = &quot;thread&quot;;

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

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

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

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

$posthandler-&gt;set_data($new_thread);

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

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

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


Please, Log in or Register to view URLs content!
 
Top Bottom