PDA

View Full Version : Create a new thread via vBulletin API


ravel123
07-13-2011, 06:22 PM
Hi,

iam trying to use this code to create a thread via vBulletin cron job:


<?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', $vbulletin, ERRTYPE_ARRAY, 'threadpost');

// Set some variable and information
$forumid = 7; // The id of the forum we are posting to
$userid = 1; // The user id of the person posting
$title = addslashes($_POST["test title bla bla test"]); // The title of the thread
$pagetext = addslashes($_POST["test content bla bla test 123 this is long enough"]); // 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_uni( fetch_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);
}

?>


It always return that the message is too short. I has to be at least 10 characters long.

Exact error message (in german): "array(2) { [0]=> string(110) "Der Text, den du eingegeben hast, ist zu kurz. Bitte erweitere den Text auf die minimale L?nge von 10 Zeichen." [1]=> string(49) "Bitte f?lle das Titel- und das Nachrichtfeld aus." } "

What am I doing wrong?

Edit: I would like to create a query to my database in this script with "$db->query_first" but there is no $db-object so i get an error. can i get the db-object from vbulletin somehow?

Thanks in advance

kh99
07-13-2011, 06:43 PM
I think your problem is with these two lines:

$title = addslashes($_POST["test title bla bla test"]); // The title of the thread
$pagetext = addslashes($_POST["test content bla bla test 123 this is long enough"]);

I don't think you want the $_POST[] in there.

I'm not sure why you had trouble with the $db object, it should be defined (unless you had your code inside a function, then you'd need a global statement).

Disasterpiece
07-13-2011, 06:51 PM
You can use $vbulletin->db, in some scripts $db is just a reference to $vbulletin->db

ravel123
07-13-2011, 07:10 PM
thank you both! everything is working right now =)

radiofranky
07-27-2011, 04:19 AM
Hi,
I would like to import threads from a csv file and I was wondering what is the best way to do it?

It contains the following columns "forum id", "user name", "title", "post text", "post date"

thanks

Disasterpiece
07-27-2011, 01:56 PM
Hi,
I would like to import threads from a csv file and I was wondering what is the best way to do it?

It contains the following columns "forum id", "user name", "title", "post text", "post date"

thanks

Take a look at the code above your post. What's not ok with it?

radiofranky
07-27-2011, 03:53 PM
thanks. How do I replace my .csv with the following fields. I mean how do I read from my csv and insert into those variables?
For +10000 thread, do I just create a loop and let looping the entire code for the number of threads?

$forumid = 7; // The id of the forum we are posting to
$userid = 1; // The user id of the person posting
$title = addslashes($_POST["test title bla bla test"]); // The title of the thread
$pagetext = addslashes($_POST["test content bla bla test 123 this is long enough"]); // The content of the thread
$allowsmilie = '1'; // Are we allowing smilies in our post
$visible = '1';

Disasterpiece
07-27-2011, 08:19 PM
Well, that's more of a php-related problem...

How is your knowledge in php?

basically all you need is fopen, fread, fclose, foreach/while and preg_match if you're good with regex, otherwise explode and trim.
All those functions can be looked-up here: http://php.net/

Scalemotorcars
08-16-2011, 02:19 PM
Can this be turned into a widget for any registered member to use.

Something like "Post a Question" and have it show up in a specific forum or thread.

vbresults
08-16-2011, 03:58 PM
Can this be turned into a widget for any registered member to use.

Something like "Post a Question" and have it show up in a specific forum or thread.

I do not mean to promote my product in any way, but do you mean like the question box on lancerforhire.com? If my assumption is correct regarding what you are looking for, you do not need to use the data manager.

Scalemotorcars
08-17-2011, 12:41 PM
Ya I still would like to get a widget like the one you mentioned but I really do need the ajax live search functionality.

The way it stands is ok but adding the feature would greatly reduce members asking the same question over and over again.

Since I didnt hear back from you I just figured that was not an option you were willing to add.

vbresults
08-17-2011, 06:45 PM
It is a quite a common request, I am just working out how to implement it in the most efficient way.

Scalemotorcars
08-18-2011, 12:35 AM
Drop me a PM when you get it going.

salman.attari
10-20-2011, 11:45 AM
Hi ,
I want to ask a question , that is it possible to import custom threads from csv file into vbulletin .

Thanks.

BirdOPrey5
10-20-2011, 03:54 PM
It would be possible if you had a script that could read the CSV fle and import the data as threads. As far as I know no one has released anything like this yet.