PDA

View Full Version : Create a thread remotely


Hereward
06-15-2008, 08:37 AM
My goal is simple. I need a PHP class which can interface with my external CMS in the following way:

1) create an article in CMS.

2) on completion of CMS submission, call a PHP function from my custom class (probably via HTTP request) which will insert a new thread into a dedicated vb forum, using the title and body of the new article - and returning to the calling app the id of the newly created forum thread.

3) provide a function which can be called from the CMS each time the page loads to get the number of replies.

I have so far found one discussion of this, here:
https://vborg.vbsupport.ru/showthread.php?t=181689

But the code sample is tantalisingly incomplete... I would love to see a full working example rather than just a snippet.

I would have thought this problem would have been solved a hundred times over by now?

Any pointers?

thanks.

Opserty
06-15-2008, 08:44 AM
<a href="https://vborg.vbsupport.ru/showthread.php?t=102418&page=3#39" target="_blank">https://vborg.vbsupport.ru/showt...2418&page=3#39</a>

Hereward
06-15-2008, 08:45 AM
my guess is that the essence is in the following:
require_once(DIR . '/includes/functions_newpost.php');
build_new_post('thread', , array(), $postinfo, $newpost, $errors);

I assume that $foruminfo, $postinfo, and newpost are simple arrays. Is there a data type definition for this stuff anywhere?

Is there any documentation for the code?

thanks (:

Opserty
06-15-2008, 08:46 AM
Check the thread datamanager files in the include directory. class_dm_thread*.php

TYPE_xxx defines their type and requirements.

--------------- Added 1213523255 at 1213523255 ---------------

Maybe of interest: Data Managers (http://www.vbulletin.com/docs/html/data_managers)

Hereward
06-15-2008, 08:50 AM
https://vborg.vbsupport.ru/showthread.php?t=102418&page=3#39

hi - thanks for that!

in the example you referenced there is a variable $vbulletin which is passed as a parameter to the datamanager_init function. How is that variable created?

Also wondering which php files I need to require for datamanager_init to work.

$threaddm =& datamanager_init('Thread_FirstPost', $vbulletin, ERRTYPE_ARRAY, 'threadpost');

$forumid = $vbulletin->GPC['fid']; // can also be a number ;) $forumdid= 12;

$foruminfo = fetch_foruminfo($forumid);
$threaddm->set_info('forum', $foruminfo);

$threaddm->set('forumid', $foruminfo['forumid']);
$threaddm->set('userid', 1234);
$threaddm->set('title', 'Testtitle');
$threaddm->set('pagetext', 'a little test');
$threaddm->set('allowsmilie', 1);
$threaddm->set('visible', 1);
$threaddm->set('dateline', TIMENOW);
$threaddm->save();

--------------- Added 1213523643 at 1213523643 ---------------

Check the thread datamanager files in the include directory. class_dm_thread*.php

TYPE_xxx defines their type and requirements.

--------------- Added 1213523255 at 1213523255 ---------------

Maybe of interest: Data Managers (http://www.vbulletin.com/docs/html/data_managers)

OK - i will start reading the manual.

--------------- Added 1213524531 at 1213524531 ---------------

$threaddm =& datamanager_init('Thread_FirstPost', $vbulletin, ERRTYPE_ARRAY, 'threadpost');

In the manual it says (in regard to the 2nd param):
$registry - the main registry object used by vBulletin to hold application-level data. In most cases, this will be the variable $vbulletin.

Do I need to use this? If so, how do I construct the object ($vbulletin)?

I am guessing that maybe I don't need it because in the sample code it is only used to fetch an id - but in my case I will be passing that as a separate param:
$forumid = $vbulletin->GPC['fid'];

So can I get away with passing an empty string as "the main registry object" or will this cause the function to fail?

--------------- Added 1213525869 at 1213525869 ---------------

OK, small question:

where does the function datamanager_init live?

This seems to be the key to it all. it would be nice if there was a working sample that showed what needs to be in the required path etc..

--------------- Added 1213526137 at 1213526137 ---------------

OK I found it in functions.php

so i guess I need to start with something like:

require $root_path/functions.php;

--------------- Added 1213526419 at 1213526419 ---------------

In the comments for datamanager_init we see:

/*
* Class factory. This is used for instantiating the extended classes.
*
* @param string The type of the class to be called (user, forum etc.)
* @param vB_Registry An instance of the vB_Registry object.
* @param integer One of the ERRTYPE_x constants
* @param string Option to force loading a class from a specific file; no extension
*
* @return vB_DataManager An instance of the desired class
*/

question: How do I construct the vB_Registry object?

--------------- Added 1213527347 at 1213527347 ---------------

OK, looks like I need something like the following:

<?php
require $root_path/includes/class_core.php;
require $root_path/functions.php;

$vbulletin = new vB_Registry();

$threaddm =& datamanager_init('Thread_FirstPost', $vbulletin, ERRTYPE_ARRAY, 'threadpost');

$forumid = $vbulletin->GPC['fid']; // can also be a number ;) $forumdid= 12;

$foruminfo = fetch_foruminfo($forumid);
$threaddm->set_info('forum', $foruminfo);

$threaddm->set('forumid', $foruminfo['forumid']);
$threaddm->set('userid', 1234);
$threaddm->set('title', 'Testtitle');
$threaddm->set('pagetext', 'a little test');
$threaddm->set('allowsmilie', 1);
$threaddm->set('visible', 1);
$threaddm->set('dateline', TIMENOW);
$threaddm->save();

?>


--------------- Added 1213529047 at 1213529047 ---------------

OK, I have no confirmed that the above DOES NOT WORK.

Can anyone tell me what is wrong with the code or should I just give up?

--------------- Added 1213531637 at 1213531637 ---------------

The following is tested and WORKS:


<?php

define('VB_AREA', 'Forum');
define('CWD', (($getcwd = getcwd()) ? $getcwd : '.'));
require_once("./includes/init.php");

define('DIR', CWD);

require_once "./includes/functions.php";

$threaddm =& datamanager_init('Thread_FirstPost', $vbulletin, ERRTYPE_ARRAY, 'threadpost');
$forumid = 30; //$vbulletin->GPC['fid']; // can also be a number ;) $forumdid= 12;
$foruminfo = fetch_foruminfo($forumid);
$threaddm->set_info('forum', $foruminfo);
$threaddm->set('forumid', $foruminfo['forumid']);
$threaddm->set('userid', 1);
$threaddm->set('title', 'Testtitle');
$threaddm->set('pagetext', 'a little test');
$threaddm->set('allowsmilie', 1);
$threaddm->set('visible', 1);
$threaddm->set('dateline', TIMENOW);
$threaddm->save();

print "The action was performed.";
?>

--------------- Added 1213531734 at 1213531734 ---------------

NOTE: The file needs to be placed at the root level of your vbulletin installation.

Opserty
06-15-2008, 02:16 PM
If you just include global.php it includes init.php and functions.php. If you want to move out of the forum root use chdir()

E.g.:

$cwd = getcwd();
chdir('path/to/forumroot/');
require_once('global.php');

$threaddm =& datamanager_init('Thread_FirstPost', $vbulletin, ERRTYPE_ARRAY, 'threadpost');

chdir($cwd);

// Continue script here...

// For error checking I think they are contained in a $threaddm->errors array.

Cryo
06-21-2008, 05:29 PM
I'm pretty much lost at this point. Here is the code I have...

// vBulletin Add Post Function (Start)

$threaddm =& datamanager_init('Thread_FirstPost', $vbulletin, ERRTYPE_ARRAY, 'threadpost');

$forumid = 17;
$foruminfo = fetch_foruminfo($forumid);

$threaddm->set_info('forum', $foruminfo);
$threaddm->set('forumid', $foruminfo['forumid']);
$threaddm->set('userid', $postuserid);
$threaddm->set('title', $title);
$threaddm->set('pagetext', $output);
$threaddm->set('allowsmilie', 0);
$threaddm->set('visible', 1);
$threaddm->set('dateline', TIMENOW);
$threaddm->save();The code worked prior to my 3.7 upgrade, now it gives this error...

Fatal error: Database object is not an object in [path]/includes/class_dm.php on line 172

I have the global.php included and directories are correct... like I said, this all worked pre-3.7. Any ideas? The output and title variables are set, by the way.

--------------- Added 1214074111 at 1214074111 ---------------

OK, so I took the code and put it into its own page and it works fine. Putting it into the page's IF statement seems to break it and gives me the error indicated above.

Dismounted
06-22-2008, 06:42 AM
Please post the whole page.

Cryo
06-29-2008, 12:50 AM
I did read the whole page. The code I have works if it is OUTSIDE the post statement, once it is inside it stops functioning (which renders it worthless). I may be missing it, but I don't see that problem addressed here. If it was, please point me to the post / segment which gives resolution.

Dismounted
06-29-2008, 12:57 PM
Please post the whole page. The whole code you are using.

Cryo
06-30-2008, 06:59 PM
Sorry, misread it, it was a long and frustrating Saturday. Anyway, I figured the problem out... a different developer polluted the code with another DB connection because he didn't realize that the global.php included a database connection. Thanks though, and sorry for the little snap. Like I said, was a long Saturday. :erm:

swellinfo
09-11-2008, 09:02 PM
I am having the same error as Cryo, and if someone might have a suggestion on what could be wrong here I would be appreciative. I had been creating threads remotely using the build_new_post. I have since upgraded to 3.7.2, and that function does not work anymore. I have been trying to follow the instructions given in this thread, but running into the following error:

Fatal error: Database object is not an object in [path]/includes/class_dm.php on line 172

Here is my include file code that I call, when someone clicks to create a new thread:

$cwd = getcwd();
chdir('/home/swellinf/www/forum/');
require_once('global.php');
$adminid = 1;
$forumid = 13;

/*
define('VB_AREA', 'Forum');
define('CWD', (($getcwd = getcwd()) ? $getcwd : '.'));
require_once("./includes/init.php");

define('DIR', CWD);

require_once "./includes/functions.php";

*/

$threaddm =& datamanager_init('Thread_FirstPost', $vbulletin, ERRTYPE_ARRAY, 'threadpost');

chdir($cwd);

$foruminfo = fetch_foruminfo($forumid);
$threaddm->set_info('forum', $foruminfo);
$threaddm->set('forumid', $foruminfo['forumid']);
$threaddm->set('userid', $adminid);
$threaddm->set('title', $title);
$threaddm->set('pagetext', $message);
$threaddm->set('allowsmilie', 1);
$threaddm->set('visible', 1);
$threaddm->set('dateline', TIMENOW);
$threaddm->save();

$threadid = $threaddm->save();


The error occurs at this line:
$threaddm =& datamanager_init('Thread_FirstPost', $vbulletin, ERRTYPE_ARRAY, 'threadpost');

I am assuming there is something wrong with the $vbulletin object I am passing. Cryo mentioned something about an additional database connection that was messing things up. Is this something I need to check? I do open a connection to my non-forum database before I include the above file.

Any suggestions here?

Thank you.

Dismounted
09-12-2008, 09:59 AM
Where are you using it in? If it is another PHP application, what is the variable for that application's database handle?

swellinfo
09-12-2008, 12:45 PM
Yes, I am using it in another php file. I don't think I have a db handle variable.

I open my db connection, just with a:
mysql_select_db();

do you have any suggestions for me from here?

Dismounted
09-13-2008, 11:48 AM
So you have 2 database connections on the same page?

swellinfo
09-13-2008, 12:23 PM
I open a DB connection before I include the file to build the new thread.

In the include file, I require_once(global.php). Does this create another db connection? If so, is this the problem. And, do you have suggestions for this?

Thanks again.

Dismounted
09-14-2008, 05:15 AM
If you can call $vbulletin->db, you have no problem - but this is the problem, the DM doesn't detect that it is an object.

When opening your own connection, you need to assign a variable to the connection - an identifier. Then use this identifier in your own queries. See the PHP manual for more info.

swellinfo
09-15-2008, 03:19 PM
My script looks something like this:

require_once('global.php');

then I get some user info from the $vbulletin object.

then I make a connection to the db like this:

$link = mysql_connect($host, $usr, $pass);
mysql_select_db($database, $link);

if ($_POST) { // the post vars pass info to create a thread remotedly
require('new_thread.php'); // this is the file i included in my first post

after the thread is created, the program goes on to make more sql db calls
}

then continues on to regular script.

Is there something I should be doing differently with the db connections?