Question

I've got a Minecraft Software written in C# that I want to send a heartbeat to my site. I've got the way to send the beat already written.

 if (Server.Uri == null) return;
        string uri = "http://GemsCraft.comli.com/Heartbeat.php";
        // create a request
        try
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            request.Method = "POST";

            // turn request string into a byte stream
            byte[] postBytes = Encoding.ASCII.GetBytes(string.Format("ServerName={0}&Url={1}&Players={2}&MaxPlayers={3}&Uptime={4}",
                             Uri.EscapeDataString(ConfigKey.ServerName.GetString()),
                             Server.Uri,
                             Server.Players.Length,
                             ConfigKey.MaxPlayers.GetInt(),
                             DateTime.UtcNow.Subtract(Server.StartTime).TotalMinutes));

            request.ContentType = "application/x-www-form-urlencoded";
            request.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
            request.ContentLength = postBytes.Length;
            request.Timeout = 5000;
            Stream requestStream = request.GetRequestStream();

            // send it
            requestStream.Write(postBytes, 0, postBytes.Length);
            requestStream.Flush();
            requestStream.Close();
            /* try
             {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Logger.LogToConsole(new StreamReader(response.GetResponseStream()).ReadToEnd());
                Logger.LogToConsole(response.StatusCode + "\n");
             }
             catch (Exception ex)
             {
                 Logger.LogToConsole("" + ex);
             }*/
        }

Now, I want to be able to retrieve the heartbeat in PHP, upload it to the SQL database, and then display each user's server in a table that will be displayed on the webpage

How do I do this?

Was it helpful?

Solution

portforwardpodcast's answer isn't very well-suited for your purposes, here's a process for you to ponder

Server accesses the following page: heartbeat.php?port=25565&maxplayers=25&players=2&name=Cheese_Pizza_Palace

Your PHP script will then do the following...

  • Go through each value, making sure they're all the types you want them to be (integers/strings)
  • Connect to the database
  • Update the server in the database if it already exists, create it if it doesn't
  • Return some value so the server knows that it completed successfully.

And to display the servers

  • Fetch all 'active' servers
  • Loop through them and display each one.

Things you'll need to figure out:

  • How to determine uptime
  • How to determine "active" servers
  • How to update/create MySQL entries
  • How to (properly) connect to a database. I would suggest using PDO since you're using PHP. It's a bit difficult to learn, but it's much more secure than writing the queries directly.
  • How to loop through all the GET variables.

Good hunting!

OTHER TIPS

I would create a simple php page accept a get variable. something like www.site.com/beat.php?lasttime=123456&serverid=1 where the number us the unix timestamp. Then you need to re-work your c# to do a simple get request on a website. Finally your php should insert into a mysql table with a column for id, timestamp, server_id etc.

First you need to pull the data from the request. The $_REQUEST variable in php is nice because it works for both GET and POST:

http://php.net/manual/en/reserved.variables.request.php

Start out by var_dump or echo the fields you want. Once you can get the needed data into variables you are done with the first part. For the next part you need to create a database and table in MySQL. The best tool for this is phpmyadmin. If you have a host like godaddy (or some others) you can get at this from the control panel. If not you may need to install upload the phpmyadmin files yourself. It's a pretty simple tool to use:

http://www.youtube.com/watch?v=xxQSFHADUIY

Once your database has the correct columns, you need to insert the data from your php file. This page should help:

http://www.w3schools.com/php/php_mysql_insert.asp

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top