Sending a request to a PHP server and getting back a response

Hello,

I have at the moment created a script which sends a :postasync() message to my php server which then inserts data into a sql server.

I am trying to find a way to retrieve data from the sql server back to roblox, is there any way to do this?

2 Likes

You would want to create another endpoint on your web server that responds with the database data you want to access in your game. If you need to specify a certain player ID for your database to look up, you can do something like http://www.yoursite.com/getPlayerLevel.php?userId=26432253. You can then use the value of $_GET["userId"] to look up their data in the database.

3 Likes

How would I get my php script to then send back the data then?

You will want to create a REST api. With a REST api you can handle GET and POST requests, therefore when sending a request from ROBLOX to your server, you want to send your data as a POST request to your endpoint. When you want to retrieve data you will want to send a GET request to your endpoint.

You’ll probably want to look at the following, although it is written using Node.js rather than PHP, it will give you an idea of what to expect. There is a section in the thread talking about REST apis.

It’s highly unlikely that people will write your code for you - if you managed to write something that inserts data, you’d be doing pretty much the same thing, just using SELECT instead of INSERT, and using the fetch_array/fetch_assoc method to echo the data back.

You’d probably be interested in JSON encoding the data on the backend, and then JSON decoding it when your Roblox server receives it.

1 Like

If I did a JSON encode and echo’d the output and json decoded it in roblox would that work

you really might be better off learning Node instead. Node is literally built for this, and glitch.me is a fantastic site for developing and hosting your API for free