[SOLVED] Sending data from FiveM to Website

Hello to whomever may be reading this,

I’m looking into the possibility to link my FiveM server up with my website in order to send data, receive data and process that data. I’m experienced in web development, and i’m rather more experienced in FiveM Lua however I haven’t been able to make a link between the two, could somebody possibly give me a hand?

I’m assuming it would be a matter of sending data via POST or GET and processing said data in PHP, inserting into sql or whatever I may need? I just don’t know how i’d do so, thank you for reading.

[SOLVED]

Thanks to someone that commented, I ended up setting up an SQL database with a WAMP server rather easily and connecting to it from my webserver.

What information you need? Like players, their names ping…

1 Like

Nono, send data included in a variable for example. Send a string, whatever

Ok cause player info you can get without hassle. There’s many ways you could do this. I’d use some html in the server scripts then from html send it to PHP etc. If it’s something you wanna log SQL would be the go for that. Just write it to SQL then retrieve.

1 Like

Thank you. I understand this is the method so to speak, but my question is more in regards to the execution of the method. Any examples, tutorials, anything really would be helpful. Sorry if I wasn’t completely clear in the original question :slight_smile:

Let’s say for example, I want to get an ETA from Point B to Point A. What i’d do is calculate time from speed and distance and assign it to a variable. Now, what I want to do is get that time in the variable and display it on a web page.

What I need to know is how do I actually do this? How do I get the variable from Server to Webserver? Or if it’s easier for everyone, how do I INERT into a MySQL database on the webserver from the game server.

Again, sorry for any confusion.

Use a POST request to your webservers API

2 Likes

Thank you. Do you have any examples of this? For receiving and sending ideally. Sorry to be a pain in the backside

You need to learn how to build an API first. It doeant just come with your webserver it needs to be writtwn and ran on your webserver.

2 Likes

Right ok, thank you mate

I think you should look in this script’s source code: [Release] LiveMap
This script is using server to web communication.

Yeah, tried that but with no direction at all as to what exactly i’m doing, it’s difficult to be able to understand what has been done to achieve it. If anyone has anything that could help me achieve my goal, i’d be much appreciated. I have no clue what i’m doing in regards to websockets or anything

It isnt something you learn over night unless you have prior code knowledge.

1 Like

Yeah I understand that, I was hoping someone would have some resources to assist in me learning over multiple nights, however many nights it takes

Javascript / NodeJS or PHP usually whatever language you are running your website with or whatever…

I am really not sure what “resources” you are looking for. Learning a language could take a LOT of hours just to learn the language by reading documents or watching videos with the exception you are practicing as you learn… Things you can do with the language usually come after you can write the language…

1 Like

I can write PHP, SQL, Lua and have an understanding of them all aswell as more on top of that. I have a decent enough understanding, all I need is some guidance as to how to go about achieving my goal. Any APIs out there already, ready to use… Any guides on how to do this. I’ve seen a few times something about PerformHttpRequest, but don’t know how this is used. I’m not after a 3 week course on Lua and PHP, i simply want guidance. A helping hand.

Well I dont know PHP you will have to do some digging to figure that out. As far as communicating woth an API the only way is to use an HTTP GET OR POST request

1 Like

Thank you all for your patience!

So far, i’ve done as much research as I can at 4am, and have managed to use performhttprequest to send the chat data to a discord server via a webhook. But now i’ve done that, what’s required of me to do exactly the same except sending the data to a webserver instead and storing it somewhere. I understand this is the FiveM forums, however this question will require some assistance with PHP.

I believe the data is being sent from the FiveM server to the Webserver (Nowhere near certain) but there’s no way I can think of to prove this because I have no idea how to then display that data.

In server.lua I have:

    AddEventHandler('chatMessage', function(source, name, message)
        PerformHttpRequest('https://###.#####/test.php', function(err, text, headers) end, 'POST', json.encode({username = name, content = message}), { ['Content-Type'] = 'application/json' })
      end)

(I censored the URL, i’m not actually putting hashtags there)

And then on the webserver, I stupidly tried simple putting this into a blank php file which I knew wouldn’t work but was keen to try it anyway

$data = json_decode(file_get_contents("php://input"), true); 
echo $data['username']; 

Again, i’m sorry for the confusion and me being a pain in the backside I just hope I can get this in the bag soon, thank you all for being so patient.

1 Like

Your data sent via POST will be in $_POST.

You need to validate this and do something with it.

1 Like

So therefore in theory, using something like

foreach ($_POST as $key => $item) {
    echo $key . ": " . $item . "<br>";
}

Would display every bit of data sent correct?

But if i’m correct, would this have to be done with AJAX or something due to the fact the page isn’t constantly updating. At the moment, if I do that after sending the data over and refresh the web page it does absolutely nothing which is to be expected I suppose.

1 Like

Would all of this be easier if I could just connect to an SQL database on a webserver? If so, how do I do that instead?