Introduction
I am struggling to work out where i have gone wrong, I have read this unity answer which explains how to post data in unity (C#) then i read this website explaining how to use get/post/request in php to receive the data. Henceforth, i came up with this code in order to post var1
with value hello
from unity and receive it on my php server code then echo it to log it and check if this has happened correctly.
C# (Unity)
using UnityEngine;
using System.Collections;
public class CommunicateWithServer : MonoBehaviour
{
void Start()
{
string url = "http://www.purelogicgames.com/index.php";
WWWForm form = new WWWForm();
form.AddField("var1", "hello");
WWW www = new WWW(url, form);
StartCoroutine(WaitForRequest(www));
}
IEnumerator WaitForRequest(WWW www)
{
yield return www;
// check for errors
if (www.error == null)
{
Debug.Log("WWW Ok!: " + www.data);
}
else
{
Debug.Log("WWW Error: " + www.error);
}
}
}
Server Code (Php using Netbeans IDE)
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<?php
if($_REQUEST["var1"])
{
echo "Recieved ". $_REQUEST["var1"]. " success!";
exit();
}
else
{
echo "Request Failed";
}
?>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
echo '<p>Hello World?</p>';
echo $_REQUEST["var1"]. " Success?";
?>
</body>
</html>
Yet still when I run the unity code in the editor I get a failed result on the web page when I open it
Unity Editor
Web Page
Netbeans IDE (not sure if it matters)
Thanks you for reading this, I imagine there's a simple solution and I have looked over countless other pages of question, etc about php unity communication, yet still i have not found it so I hope this isn't a time wasting repeat of another question and also as it's in simplified terms it could prove a useful example for others. So feel free to ask any questions and hopefully answer my question. Just to put this in some context, this is my first time using PHP properly (hence there still is a hello world in my code) and I am used to using C# in unity and would describe my general programming knowledge as rather limited. Thanks.