Simple and easy PHP and Unity POST and GET http request tutorial

This tutorial is about simple and easy HTTP POST and GET request from Unity.

Watch this video to see how it works:

First, take a look this php program, you can test sending POST and GET request directly from web browser if you are running it on your localhost server.

<?php
if(isset($_GET["unityget"])){
	echo "This is Unity GET Response. Entered value is: " . $_GET["unityget"];
}else if(isset($_POST["unitypost"])){
	echo "This is Unity POST Response. Entered value is: " . $_POST["unitypost"];
}else{
	?>

	<!DOCTYPE html>
	<html>
		<head>
			<title>Unity PHP POST and GET</title>
			<style>
				body{
					background-color: black;
				}
				.box{
					background-color: white;
					margin: 10px;
					padding: 10px;
				}
			</style>
		</head>
		<body>
			
				<div class="box">
					<h1>POST</h1>
					<form method="post">
						<input placeholder="Type something..." name = "unitypost"><input type = "submit" value = "Submit">
					</form>
				</div>
				<div class="box">
					<h1>POST</h1>
					<form method="get">
						<input placeholder="Type something..." name = "unityget"><input type = "submit" value = "Submit">
					</form>
				</div>
				
		</body>
	</html>

	<?php
}
?>

With that program you can simply type some text on POST and GET request forms and click submit button to get the response.

Now what about Unity? Below is the code example for sending POST and GET request from Unity.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;

public class UnityPostGet : MonoBehaviour
{
    public InputField PostInput;
    public InputField GetInput;

    public void SendPostRequest()
    {
        string Message = PostInput.text;
        StartCoroutine(SendPR(Message));
    }

    public void SendGetRequest()
    {
        string Message = GetInput.text;
        StartCoroutine(SendGR(Message));
    }

    IEnumerator SendPR(string Message)
    {
        WWWForm form = new WWWForm();
        form.AddField("unitypost", Message);
        using (UnityWebRequest www = UnityWebRequest.Post("http://localhost/tutorials/unityphppostget/", form))
        {
            www.downloadHandler = new DownloadHandlerBuffer();
            yield return www.SendWebRequest();

            if (www.isNetworkError)
            {
                Debug.Log(www.error);
            }
            else
            {
                string responseText = www.downloadHandler.text;
                Debug.Log("Response Text from the server = " + responseText);
            }
        }
    }

    IEnumerator SendGR(string Message)
    {
        using (UnityWebRequest www = UnityWebRequest.Get("http://localhost/tutorials/unityphppostget/?unityget=" + Message))
        {
            yield return www.SendWebRequest();

            if (www.isNetworkError || www.isHttpError)
            {
                Debug.Log(www.error);
            }
            else
            {
                string responseText = www.downloadHandler.text;
                Debug.Log("Response Text from the server = " + responseText);
            }
        }
    }
}

I’ve prepared all the files needed to run this example in your Unity project. Below is the link to download a UnityPackage including above C# code and also PHP code.

To download the files, click here.