- Home /
send data from embedded game through PHP?
Hi, I'm looking to upload and embed my game to an online website, and have found an online host simmer.io to upload my game. However I want to send the game data through a PHP script to write the game data to a local file.
I followed this tutorial from Holistic3D on reading and writing to a text file from unity webGL (https://www.youtube.com/watch?v=4OZqY1Ukj8I) and got this piece of code that supposedly talks to the PHP server.
public void saveScores()
{
StartCoroutine(sendTextToFile());
SceneManager.LoadScene("Close prompt");
}
IEnumerator sendTextToFile()
{ bool successful = true;
WWWForm form = new WWWForm();
form.AddField("Name", participantID);
form.AddField("MC_attempts", MC_attempts);
form.AddField("MC_time_taken", MC_time);
form.AddField("Ped_attempts", Ped_attempts);
form.AddField("Ped_time_taken", Ped_time);
form.AddField("Level_order", levelOrder);
WWW www = new WWW("https://simmer.io/@Pancy/Pelmanism/sendfromunity.php", form); //line51
yield return www;
if (www.error != null)
{
successful = false;
}
else
{
Debug.Log(www.text);
successful = true;
}
}
and then I've got this PHP code that should write the data to a text file:
<?php
$Name =$_POST["Name"];
$MC_attempts = $_POST["MC_attempts"];
$MC_time = $_POST["MC_time_taken"];
$Ped_attempts = $_POST["Ped_attempts"];
$Ped_time = $_POST["Ped_time_taken"];
$levelOrder = $_POST["Level_order"];
if ($Name != "")
{
echo("Message successfully sent");
echo("Field 1: " . $Name);
echo("Field 6: " . $levelOrder);
$file = fopen("webgl_data.txt", "a");
fwrite($file, $Name." ".$MC_attempts." ".$MC_time." ".$Ped_attempts." ".$Ped_time." ".$levelOrder."\r\n");
fclose($file);
}
else
{
echo("Message delivery failed");
}
?>
It works when I'm replacing the webform as http://localhost:8012/sendfromunity.php in line 51, but after uploading my game the local host url doesn't work. I've tried changing it to the url of my game but it still couldn't work.
I'm aware I might have made some silly mistakes or might have even misunderstood the entire tutorial; so if anyone could point me in the right direction I would be eternally grateful!
Your answer
Follow this Question
Related Questions
Make a Register/Login page using html and then use it to log into a Unity Game,Hi! 1 Answer
How create customize bg on facebook, unity WebGl? 0 Answers
Itch.io webgl not uploading properly 1 Answer
Webgl browser error " DirectoryNotFoundException: Could not find a part of the path" 0 Answers
Embedded HTML for Unity WebGL build 0 Answers