XF 2.0 How to create thread from PHP?

jjff

Member
Hi, I'm trying to integrate a customer support request system into my forum.

I'd like to create a discussion thread as soon as the customer hits submit on a form.

How do I create a XF discussion thread from my PHP script?

Is there a programming guide somewhere that has these cookbook solutions for XF?

Any pointers very much appreciated.
 

Mr Lucky

Well-known member
Hi, I'm trying to integrate a customer support request system into my forum.

I'd like to create a discussion thread as soon as the customer hits submit on a form.

How do I create a XF discussion thread from my PHP script?

Is there a programming guide somewhere that has these cookbook solutions for XF?

Any pointers very much appreciated.
I may be misunderstanding, but wouldn't it be simpler for them to just start a thread (or conversation)?
 

Mr Lucky

Well-known member
Perhaps so, but I don't get to choose.

Anyone got any tips on how to do this?
I still don't get it. If the support forum has required custom thread files, it's basically a form that starts a thread when it's submitted, which is what you are asking. My apologies if I am barking totally up the wrong tree.
 

jjff

Member
I still don't get it. If the support forum has required custom thread files, it's basically a form that starts a thread when it's submitted, which is what you are asking. My apologies if I am barking totally up the wrong tree.
This part of the system is not under the forum.

It's totally outside the forum system.

I thought of simply entering a row into xf_threads and xf_posts tables but I'm guessing there's a way to do it using the existing XF base?
 

Chris D

XenForo developer
Staff member
I still don't get it. If the support forum has required custom thread files, it's basically a form that starts a thread when it's submitted, which is what you are asking. My apologies if I am barking totally up the wrong tree.
Often when people post for specific development support, they've settled on approach for various reasons and they would just like help with that, rather than having other people analyse it further.

Is there a programming guide somewhere that has these cookbook solutions for XF?
If you're aiming for XF 2.0 then you would have to use the XF framework directly for this. We have an approach which makes use of reusable services to achieve many common tasks, including creating a thread.

We create threads in a number of contexts ranging from obviously someone making a post, to more automated approaches such as creating a new thread from RSS feeds or creating new threads from users reporting content (if that option is enabled) etc.

To initiate the XF framework, you just need a few lines of code:
PHP:
$dir = '/path/to/your/xf/root';
require ($dir . '/src/XF.php');

XF::start($dir);
$app = XF::setupApp('XF\Pub\App');
Then you can check out src/XF/Service/Feed/Feeder.php and the importEntry method specifically for an example of how to create a thread.

If you wanted to wait until XF 2.1 then it will be easier to use the new built in REST API.
 

jjff

Member
Often when people post for specific development support, they've settled on approach for various reasons and they would just like help with that, rather than having other people analyse it further.


If you're aiming for XF 2.0 then you would have to use the XF framework directly for this. We have an approach which makes use of reusable services to achieve many common tasks, including creating a thread.

We create threads in a number of contexts ranging from obviously someone making a post, to more automated approaches such as creating a new thread from RSS feeds or creating new threads from users reporting content (if that option is enabled) etc.

To initiate the XF framework, you just need a few lines of code:
PHP:
$dir = '/path/to/your/xf/root';
require ($dir . '/src/XF.php');

XF::start($dir);
$app = XF::setupApp('XF\Pub\App');
Then you can check out src/XF/Service/Feed/Feeder.php and the importEntry method specifically for an example of how to create a thread.

If you wanted to wait until XF 2.1 then it will be easier to use the new built in REST API.
Thanks very much for this awesome reply. This is exactly what I needed.

I'm following the 2.1 announcements as well, but our upgrade cycle is not up to me so I don't really have any ideas when the project leaders will want to move to 2.1. For the time being I'll have a shot at this approach and will use it to become more familiar with the XF internals as well.
 

Chris D

XenForo developer
Staff member
It's worth noting that XF 2.1 pretty much signals that XF 2.0 is end of life automatically (XF 1.5 was a special case for obvious reasons) so worth doing sooner than later.

But getting your hands dirty in the code isn't a bad thing :)
 

jjff

Member
It's worth noting that XF 2.1 pretty much signals that XF 2.0 is end of life automatically (XF 1.5 was a special case for obvious reasons) so worth doing sooner than later.

But getting your hands dirty in the code isn't a bad thing :)
Will code written using the API used in the feed import function you suggested be deprecated in 2.1? What parts of the interface will remain?
 

Chris D

XenForo developer
Staff member
All of those approaches are still valid and will remain.

The REST API coming in 2.1 just allows you to perform certain actions (listing threads, posting threads, creating posts etc) without writing code in the usual way - you can perform the actions using HTTP requests.

For example, creating a thread is as simple as sending a POST request (with an API key) with various parameters such as the node ID, title and message to /api/threads/.
 

jjff

Member
All of those approaches are still valid and will remain.

The REST API coming in 2.1 just allows you to perform certain actions (listing threads, posting threads, creating posts etc) without writing code in the usual way - you can perform the actions using HTTP requests.

For example, creating a thread is as simple as sending a POST request (with an API key) with various parameters such as the node ID, title and message to /api/threads/.
Looking forward to the REST version indeed, it's probably going to be one of the most popular features you guys have ever released (as can be noted on the replies on the have you seen section lately).

For now you've solved my main problem which was reaching XF from outside its root.

One last thing (sorry for taking your time) : I now have a $app returned from XF::setupApp

Where can I see a reference for all the things $app can do?

In my specific case, from what I see, I must now get a XF:Thread\Creator and so on.

I don't want to bother you asking about specific cases, I guess I can probably figure this out from here. So if I were to develop this without that code reference you gave me, where would I go to see everything that $app can do? Read the source? Or is there some reference somewhere? Sorry if dumb question.

Thanks again.
 

OperaManiac

Well-known member
For example, creating a thread is as simple as sending a POST request (with an API key) with various parameters such as the node ID, title and message to /api/threads/.
ah cool. this should help for a query i posted a while back!

 
Top