Objective
Unity3d is a cross-platform engine, in the mobile internet wave, especially in the 3d direction of mobile games, Unity3d is undoubtedly the most dazzling star. Unity3d HTTP communication is very simple, and very easy to use, the following HTTP and PHP sever interaction to briefly introduce.
Principles of HTTP Submission data
The HTTP protocol obtains and submits data by URL. There are two ways to submit data, one is the Get method, the other is the Post method. Get is typically used to tell the server to send data that satisfies the parameter back.
For example: Get's HTML code is as follows:
[HTML] View plaincopy
<form action= "search.php" method = "Get" >
<username:<inputtypeinputtype= "text" name= "user"/>< br>
<password:<inputtypeinputtype= "password" name= "pwd"/><br> <input
"Submit" value= "Login"/>
</form >
Post typically sends data to the server, which the server processes, such as storage to a database.
For example, the HTML code for post is as follows:
<form action= "login.php" method = "POST" >
<username:<inputtypeinputtype= "text" name= "user"/>< br>
<password:<inputtypeinputtype= "password" name= "pwd"/><br> <input
"Submit" value= "Login"/>
</form >
In fact, the difference is the submission of the same way, click the login button, the browser address bar, respectively, shown as follows:
The Get method URL is: http://127.0.0.1/serach.php?user=hortor&pwd=123
Post Method URL is: http://127.0.0.1
PHP server-side Receive data method
There are two ways that the server responds to both of these ways:
The Get method receives the data by $_get[user] to receive the value of the user sent by the client.
The method that the POST method receives data is $_post[user] is also the value used to receive the client variable user.
UNITY www class use method
The Unity WWW class also corresponds to two ways to submit data, and the two constructors commonly used are:
static function www (url:string): www
static function www (url:string, form:wwwform): www
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/
The first function is to send data to the server through a Get method, and the second is to submit the data by post, where Wwwform has a method called: AddField (agr:string, value:string) to add parameters.
Get Example:
#pragma strict
Privatevar url:string = "127.0.0.1/login.php?user=test&pwd=123";
function Start () {
var getdata:www = WWW (URL);
Yield getData;
if (getdata.error!= null) {
Debug.Log (getdata.error);
}
else {
Debug.Log (getdata.text);
}
}
Post Example:
#pragma strict
Privatevar url:string = "127.0.0.1/login.php";
function Start () {
var form:wwwform = new Wwwform ();
Form. AddField ("User", "test");
Form. AddField ("pwd", "123");
var getdata:www = WWW (url, form);
Yield getData;
if (getdata.error!= null) {
Debug.Log (getdata.error);
}
else {
Debug.Log (getdata.text);
}
}
Where yield this method is called once per frame of the program, when the GetData execution completes, returns the result, which is equivalent to the asynchronous request data. The WWW class has several commonly used static variables, respectively:
1, WWW.text return from the Web page to get data, type string.
2, WWW.error return error messages, such as timeout, network connectivity errors and so on.
Author: csdn Blog Unity3d Learner