PDA

View Full Version : Automatic posts creation on web forum.



wodzu
29-12-2007, 11:47 AM
Hello folks.

I've just setup myself a phpBB forum. What I would like to do is to connect to it via Delphi and automaticaly start / writting threads. Any idea how to do this? Seems like a lot of work :roll:

Senap
29-12-2007, 01:15 PM
In my opinion, it would be easier to create a custom PHP document that parses input from your Delphi app (using PHP's $_POST variables) and then adds this info to your phpBB mySQL database. There is a security risk in accepting any input but you'll have to do some sort of security check before posting data of course.

I suppose you know how send this data using Delphi but I still found this thread for you, in case you don't:
http://www.delphipages.com/threads/thread.cfm?ID=97550&G=97434

I hope this helps you :D

wodzu
29-12-2007, 01:52 PM
Thanks Senap.

I was thinking also about the solution you mentioned. I am not experienced in network programing and I was using PHP for the last time like 4-5 years ago :?

I am not sure if I understand this correctly:

1.I am posting some document to the server.
2.I have some PHP script which then parses this document and inserts data to the SQL database.

Is this correct?

But how my server will know that something has been posted? I must write also some script that will check from time to time if such document exists on the server?

Senap
29-12-2007, 02:34 PM
It's more of an automated process since PHP (basically) only works when it's accessed. The solution I posted is similar to posting a HTML form to the PHP doc.

Here's a sample of the PHP doc you could write:


// check if variable NAME has been posted (i.e. exists)
if isset($_POST['name']){
// assign to a normal PHP variable, not really necessary
$name = $_POST['name'];

// make mySQL query and save to database
$query = "INSERT INTO mytable VALUES ('$name')";
$result = mysql_query($query);

// result
if ($result){
echo 'Success!';
} else {
echo 'Fail';
}
}



That's the PHP code I came up with just now so it might not be correct or compile, but I think it will. You'll need to connect to the mysql database first of course, before you send the query. Try saving it as delphi.php and upload this once when you're finished and just leave it on the webserver.

Assuming you followed the tutorial on delphipages, you'll send these variables to PHP

Params.WriteString(URLEncode('teste=' + 'yes' + '&'));
Params.WriteString(URLEncode('name=' + 'ivan' + '&'));
Params.WriteString(URLEncode('number=' + '102'));

Now, let's say that your PHP file is in www.mysite.com/delphi.php

change this line so it has the correct address

try
Post('http://www.mysite.com/delphi.php', Params, aStream);
except
on E: Exception do
showmessage('Error encountered during POST: ' + E.Message);
end;

Now, each time you run this code in Delphi, it will post the name ivan along with the other variables to delphi.php and the PHP doc will get the name variable and post it to mysql. PHP is always "standing by" so you just post the info you need whenever you want from the Delphi app and PHP will take care of it.

Hopefully this will clarify a few things :D

wodzu
29-12-2007, 04:18 PM
Woho! Thanks mate, now everything is clear :D

Just two more questions:

I guess there is some PHP function which allow me to log in / connect to the database? Do you happen to know one? ;)

Once again thanks :)

Senap
29-12-2007, 04:35 PM
I seldom write the connection function, I just re-use a standard function I wrote years ago for all my sites - so I don't remember how to do it :lol:

But I found the basic code that does the trick at this site
http://www.freewebmasterhelp.com/tutorials/phpmysql/2


$user="username";
$password="password";
$database="database";
mysql_connect(localhost,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");


Remember to put

mysql_close(); at the end of "delphi.php". Having a bunch of mysql connections open is not good if you have a lot of traffic/access the site a lot.

Good luck :D

cairnswm
29-12-2007, 06:13 PM
In the tutorials forum there is a post by myself on how to get Delphi to complete http forms. This would allow you to just use delphi from your desktop to complete the php form to make a post.

wodzu
29-12-2007, 08:09 PM
Thanks cairnswm.

Tutorial is quite handy but it is hard for me to figure out what to send via HTTP to the forum. Since it is phpBB3 forum it requires a lot of things, like cookie for example :?

So basically I have two solutions:

1. Insert a post by my own using SQL which unfortunately is not a trivial task (connect all those tables, insert some hash values... unless i find some documentation :? )
2. Send all the things via HTTP and once again I don't know every data which is required.

Damn ;)

Senap
29-12-2007, 09:07 PM
Yeah, that's the main problem. Since forums and CMS (Content Management Systems) usually are very complex, it's hard to know what info to "feed" them to make a simple post.

Is the program designed to allow people to post messages on your forum? If not, I would suggest you make a custom mySQL table and PHP page (one that accepts posts from the app, like we discussed, and that also displays the data as HTML) to go with your application. It would be easier and less complex that way IMHO.

wodzu
29-12-2007, 10:32 PM
Senap forum is designed to allow post messages(but the program no).

I just wanted to have multiple forums, each for the application which I am making for my company. In those forums I wanted to have sections with documentation, bug tracking and so on. I tought it would be nice to write a parser for my pascal code which could take comments for each function(for example) and than generate from it post on the forum. It would be changed dynamically every time I rebuild my application.

Those all table realtions might be hard but I was having idea of tracing post creation. Just don't know the tool which allow me to trace SQL queries on the server. I've never used MySQL (rather MS SQL or Firebird at work). Does phpMyAdmin has such possibility?
After tracing executed commands on the server I could then reapeat them on my query ;)

And maybe it would work, maybe ;)

The idea with separate table doesn't sounds so bad but.. but...the oryginal idea would be so cool 8) ;)

I still belive it can be done ;)

Senap
30-12-2007, 06:45 PM
Sorry, I couldn't post earlier because I couldn't access the site. Here's what I tried to post yesterday:

Sure it can be done, it's just a matter of figuring out what needs to be posted to the phpBB mySQL database, as we said earlier. It sounds like (and is) a pain though, hehe, as the tables are probably "nested" and I suppose you need to post info to several tables to make one post. I'm just guessing though.

I would still advice you to do your own thing from scratch. It's easier to customize compared to "hacking" phpBB. You can still make it so that people can post comments to your entries made with your Delphi app.

The link I gave you earlier (with the mysql connection code) has a good tutorial on how you can set up a mysql table and posting data to it. It also shows how you can read data from it and display it using PHP. phpMyAdmin is a good tool to use to monitor and edit the mySQL database.

You just need three tables really, one for the data that is posted from the delphi app, another for user accounts and the third for comments.

wodzu
01-01-2008, 08:45 PM
Thanks Senap:)

Some nice guys from phpBB site told me about its database structure so I am gonna try with TIdHTTP and those queries which they gave me.

Here is the topic if someone is interested:

http://www.phpbb.com/community/viewtopic.php?f=46&t=659155&p=3694885#p3694885

Senap
02-01-2008, 05:41 AM
Great, good luck with this :D

wodzu
04-01-2008, 11:48 PM
Thanks.

I am slowly working on this, learning PHP on the way ;-)