Skip to content

ThirteeNov

My personal blog about coding and internet

Menu
  • About me
  • About Zofia Kreasi
  • Cart
  • Checkout
  • Making an airplane game from scratch in Unity
  • My account
  • Privacy Policy
  • Privacy Policy – zkLeaderboard
  • Sample Page
  • Shop
  • Tutorials on Learning JavaScript
  • ZKAccounts – Privacy Policy
Menu

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

Posted on December 1, 2019August 10, 2020 by Habibie

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.

Post Views: 862
ciihuy2020

Welcome!

  • My YouTube Channel
  • My GitHub Page
  • About me

Categories

  • 3DVista
  • Android
  • Apache
  • C#
  • Cordova
  • Electron & Node JS
  • HTML5, CSS & JavaScript
  • iOS
  • Let's Make Unity Games
  • Misc
  • Photoshop
  • PHP
  • Python
  • Uncategorized
  • Unity
  • WordPress

Recent Posts

  • Hover Reveal script for Unity to show and hide object on mouse hover
  • How to Prevent UI Clicks in Unity from “Bleeding Through” to 3D Objects Behind Them
  • Make objects like wires and cables easily in Unity using Ciihuy Curved Mesh
  • [SOLVED] Can’t Add Custom Domain to Blogger After Losing CNAME Verification
  • iOS App Icon Generator by CiihuyCom
© 2026 ThirteeNov | Powered by Superbs Personal Blog theme