Go Back   vb.org Archive > vBulletin Article Depository > Read An Article > vBulletin 3 Articles
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools
Creating a New Thread using vBulletins API
megamoose's Avatar
megamoose
Join Date: Feb 2007
Posts: 19

 

Northern Ireland
Show Printable Version Email this Page Subscription
megamoose megamoose is offline 12-05-2009, 10:00 PM

I really hope this will help some of you, as I spent ages searching the Internet tring to find something to do this, and ended up having to write my own.

This basically does exactly what the title says. It creates a new thread in a forum on your vbulletin forum.

It works for me in the latest version of vBulletin, but I'm not sure how far back it will work. But you should have your vBulletin up to date anyway.

If you notice any bugs/problems or anything I have done wrong, please correct me.

PHP Code:
<?php
/*======================================================================*\
|| #################################################################### ||
|| # Create a new thread                                              # ||
|| # ---------------------------------------------------------------- # ||
|| # Hopefully this will help some of you people who are needing to   # ||
|| # create a new thread externally using vBulletins API. Good luck!  # ||
|| #################################################################### ||
\*======================================================================*/

// ####################### SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE & ~8192);

// Include vBulletin runtime files
require_once('./global.php');
require_once(
'./includes/functions_databuild.php');

// Create a new datamanager for posting
$threaddm =& datamanager_init('Thread_FirstPost'$vbulletinERRTYPE_ARRAY'threadpost');

// Set some variable and information
$forumid 43;                                                                  // The id of the forum we are posting to
$userid 125500;                                                                 // The user id of the person posting
$title addslashes($_POST["title"]);                                           // The title of the thread
$pagetext addslashes($_POST["content"]);                                      // The content of the thread
$allowsmilie '1';                                                             // Are we allowing smilies in our post
$visible '1';                                                                 // If the post visible (ie, moderated or not)

// Parse, retrieve and process the information we need to post
$foruminfo fetch_foruminfo($forumid);
$threadinfo = array();
$user htmlspecialchars_unifetch_userinfo($userid) );

$threaddm->set_info('forum'$foruminfo);
$threaddm->set_info('thread'$threadinfo);
$threaddm->setr('forumid'$forumid);
$threaddm->setr('userid'$userid);
$threaddm->setr('pagetext'$pagetext);
$threaddm->setr('title'$title);
$threaddm->set('allowsmilie'$allowsmilie);
$threaddm->set('visible'$visible);

// Lets see what happens if we save the page
$threaddm->pre_save();
if(
count($threaddm->errors) < 1) {
    
// Basically if the page will save without errors then let do it for real this time
    
$threadid $threaddm->save();
    unset(
$threaddm);
} else {
    
// There was errors in the practice run, so lets display them
    
var_dump ($threaddm->errors);
}

?>
Thanks to ragtek for the revised code!
Reply With Quote
  #2  
Old 12-06-2009, 07:46 PM
Lynne's Avatar
Lynne Lynne is offline
 
Join Date: Sep 2004
Location: California/Idaho
Posts: 41,180
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

When would you use this? Like in a cron job (scheduled task) to create a new thread?
Reply With Quote
  #3  
Old 12-06-2009, 08:05 PM
megamoose's Avatar
megamoose megamoose is offline
 
Join Date: Feb 2007
Location: Northern Ireland
Posts: 19
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Lynne View Post
When would you use this? Like in a cron job (scheduled task) to create a new thread?
You can use this for a number of things like I have a database of jokes so I am going to run a cron every X hours to create new threads with the jokes in it. There are loads of uses for it like you can use it to let an external script (like a blog) create a new thread.

This is just a handy piece of code that should let anyone create a script around it (if they know a bit of PHP) to create threads for whatever reason they may have!
Reply With Quote
  #4  
Old 12-07-2009, 07:33 AM
ragtek ragtek is offline
 
Join Date: Mar 2006
Location: austria, croatia
Posts: 1,630
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

1. you don't need to inlcude this files:
PHP Code:
require_once('./includes/class_dm.php');
require_once(
'./includes/class_dm_threadpost.php'); 
it's happening automatic
2.
why you eval this part?:
PHP Code:
eval('$title = "' addslashes("THE THREAD TITLE") . '";');                     // The title of the thread
eval('$pagetext = "' addslashes("THE CONTENT OF THE POST") . '";');           // The content of the thread 
Reply With Quote
  #5  
Old 12-07-2009, 04:13 PM
megamoose's Avatar
megamoose megamoose is offline
 
Join Date: Feb 2007
Location: Northern Ireland
Posts: 19
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thanks ragtek, have edited my code and post. I evaled it because I thought to make it secure from SQL exploits you had to. Maybe it was something in a previous vB version, I'm not sure. I've removed it and it's still working fine, and the addslashes still works.

Thanks!
Reply With Quote
  #6  
Old 12-29-2009, 12:22 AM
tms1791 tms1791 is offline
 
Join Date: Mar 2007
Posts: 5
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thanks for making it painless.

You might want to replace the addslashes() calls with mysql_real_escape_string() though.

Thanks again!

--------------- Added [DATE]1262068851[/DATE] at [TIME]1262068851[/TIME] ---------------

I spoke too soon (about it being painless)...

I have a site where the forums are secondary to the rest of the site, which is also database driven. What I first tried to do was incorporate the above code into my admin interface so that when I added or edited a record in the database, the details of that record would be posted as a new thread. It almost worked, but I was getting an error that I couldn't get around, and googling it led me to believe that it had to me having an existing database connection when I tried to load the vB includes.

No big deal, right? I'll just put the code into a separate script, and POST a request to that script, so it'll be completely independent of my own stuff. Kind of clunky, but whatever - as long as it works. It didn't. Sort of.

I'm able to create a new thread, and the title & content are there, but the body of the post is showing the \n (carriage returns) instead of converting them to.. carriage returns. So instead of this:
Quote:
The quick brown
fox jumped over
the slow
lazy dog.
I get this:
Quote:
The quick brown\nfox jumped over\nthe slow\nlazy dog.
in the post.

I added some debugging code to make sure that I'm POSTing the data correctly and that it's coming through on the other side properly, and everything seems hunky dory. I also tried running nl2br() on the content data, but then I just get:
Quote:
The quick brown<br />fox jumped over<br />the slow<br />lazy dog.
in the body of the post.

It seems like it should be totally trivial - and maybe it is (I've been coding all day with very little sleep last night) - but I can't figure it out.

What am I missing? (And before anyone asks, yes, I'm enclosing the \n's in double quotes. )

--------------- Added [DATE]1262108422[/DATE] at [TIME]1262108422[/TIME] ---------------

Ok, after some sleep, I realized what was happening.

Don't use my suggestion of replacing addslashes() with mysql_real_escape_string() - it was escaping the carriage returns and literally inserting a \n into the text.

All is right in the universe now.
Reply With Quote
  #7  
Old 04-21-2010, 11:41 AM
NolanChart NolanChart is offline
 
Join Date: Apr 2010
Posts: 5
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Very nice script!

One question: once created, how can I retrieve the newly created thread id?
Reply With Quote
  #8  
Old 04-21-2010, 11:50 AM
ragtek ragtek is offline
 
Join Date: Mar 2006
Location: austria, croatia
Posts: 1,630
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

PHP Code:
$threadid $threaddm->save(); 
Reply With Quote
  #9  
Old 04-21-2010, 11:59 AM
Digma Digma is offline
 
Join Date: Nov 2004
Location: Netherlands
Posts: 60
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Why I have never spotted this thread is a riddle to me. I ploughed through dozens of them and ended up doing roughly the same thing as you. Even posted a question if my working code was complete like that in the vb3 programming discussion forum. *sigh*..
Reply With Quote
  #10  
Old 04-21-2010, 12:20 PM
NolanChart NolanChart is offline
 
Join Date: Apr 2010
Posts: 5
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by ragtek View Post
PHP Code:
$threadid $threaddm->save(); 
Boy, do I feel dumb!
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 07:10 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04951 seconds
  • Memory Usage 2,328KB
  • Queries Executed 25 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (5)bbcode_php
  • (5)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)modsystem_article
  • (1)navbar
  • (4)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (2)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (9)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete