Receiving input from Server to Unity Webplayer

I created a web application that uses a php file on server to store and process value. In my web application I embedded Unity web player as means of visualisation.

When I run my program from server, and execute php file manually via browser, I can see that I am using same sessionid.
Although they are on the same session id, and I am able to get reply from server in my html element but Unity seems ignoring input from server. Could someone tell me what am I missing? Am I using the WWW correctly? I’m not getting any errors on my end. No errors in Chrome’s dev tool. No error in Unity.

In my unity I use:

function Listen() {
	while(true) {
		yield WaitForSeconds(0.15);
		ListenForNewMessage();
		
	}
}
function ListenForNewMessage() {
	
	// if we have it, use it to retreive information
	var url = "http://localhost/colourChanger.php";
	var www : WWW = new WWW (url);
 
	// wait for request to complete
	yield www;
 	
	// and check for errors
	if (www.error == null)
	{
    // request completed!
	} else {
    // something wrong!
    	Debug.Log("WWW Error: "+ www.error);
	}
	
	if(www.text == "Red" ||	www.text == "Green" ||
	www.text == "Yellow" ||	www.text == "White") {
		var component = sphere.GetComponent(ColourSwitcher);
		component.SwitchColor(www.text);
		message = "Received: " + www.text;
	} else {
		Debug.Log("No Input");
		message = "No input. " + www.text; // will get the sessionid instead as per php code
	}

}

My php contains:

<?php

session_start();
$sessionid = session_id();

echo $sessionid;



  $ballColour = "White";
  $_SESSION['ballColour'] = $ballColour;

//----Change ball colour----//
if (isset($_POST['ballColour'])) {
    $_SESSION['ballColour'] = $_POST['ballColour'];
    echo $_SESSION['ballColour'];
} else {
    
}
?>

WWW makes a GET request to the server. Your PHP assumes a POST request.

Ack! I stared at it for a long time and didn’t see it coming.

Maybe not necessary, here, but useful note for anyone reading up on the WWW class: depending on which constructor overload you use, you can use it to send POST requests. The WWWForm class is handy for most basic use cases.

Hold on a sec, I am somewhat confused. As far as I understand, WWW is get, and WWWForm is post. I am making unity to GET data from the server. If I use POST, then I will be submitting something, which I don’t want to do. (confused…)

You can use POST and not send anything.

(What confused me is that you say your PHP works if you hit it with a browser, but browsers make GET requests. I don’t understand why you’ve written PHP that uses $_POST if you actually want to request data using GET.)

Hi Graham,

I was under impression that POST means you must post (make modification/entry) something. After digging through PHP and Unity docs, it seems that you don’t need to send anything to POST. Oh, it works in my browser because I use $.ajax withtype: 'POST'