1

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

enter image description here

Web Page

enter image description here

Netbeans IDE (not sure if it matters)

enter image description here

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.

g_l
  • 201
  • 1
  • 6
  • 15
  • 1
    Not exactly sure how PHP works, but in ASP.NET what you are doing simply makes no sense. Unless in PHP you are magically saving the sent data (which I don't see that happening), when you navigate to the page it will be an entirely new request and won't have the POST data so it will always be a failure – Camilo Terevinto Jun 26 '17 at 12:05
  • In order to check it with your browser, add "?var1=hello" to the address – Tamar Jun 26 '17 at 12:06
  • I don't understand why there is HTML code –  Jun 26 '17 at 12:07
  • For "post" method you have to use a development tool or create an html form – Tamar Jun 26 '17 at 12:07
  • @CamiloTerevinto Yes I wondered that but I thought from other examples it may work, is there a function which is called when there is a new post received? Or an update function similar to unity in PHP? – g_l Jun 26 '17 at 12:09
  • Not entirely sure where the request is fired, but the C# portion seems to log an "OK" which means the response that it got from PHP was 200. however that PHP code is not designed to fail if there's no valid input given so I'd suggest you do that (e.g. set the response status to 400) and make the response just some string so the log entry looks easier to read. no point in making it a full HTML page if you just need to read a line. – apokryfos Jun 26 '17 at 12:10
  • @3000 Yep that's there from when I first did a hello world post, so I left it in as a example framework, if it helps I can get rid of it – g_l Jun 26 '17 at 12:11
  • https://stackoverflow.com/questions/36770425/get-data-from-php-into-c-sharp-script –  Jun 26 '17 at 12:13
  • @apokryfos Hi, I will be honest I don't know what you are talking about when you mention 400 and 200, in my code my aim was to make the simplest way to pass a variable from until c# code to PHP, but I do need to widen my knowledge first, what would you suggest as a starting phrase to Google or do you know a good tutorial/guide for such? – g_l Jun 26 '17 at 12:16
  • https://en.wikipedia.org/wiki/List_of_HTTP_status_codes 200 is "OK" which means all is well. 400 means "Bad request" in this case indicating that the server was expecting var1 but didn't get it. Any code above 400 must trigger the error event for any decently written HTTP client (I'm guessing `WWW ` is one). In php you can set the response code by `http_response_code(400)` otherwise it just sends a 200 by default. – apokryfos Jun 26 '17 at 12:19
  • @Tamar I am not sure how that checks that the php server has received var1 as 'hello' as when you type http://www.purelogicgames.com/index/?var1=hello into a browser and replace 'hello' with any other string you get the same result of success and how could you log this in the PHP script? Again, I doubt I understand the importance of that link so if you know a link of guide/tutorial or a phrase to guide Google searches I would be grateful – g_l Jun 26 '17 at 12:34
  • @apokryfos Thanks, I understand the HTTPS log values, not sure of their relevance though. So to answer your previous questions the request is sent from the 'WaitForRequest()' function which is part of Unity Engine so you may be unfamiliar if you know C# and it can be easily googled, also the PHP code is I think designed to fail when $_Request["var1"] doesn't return true, or is empty in the echo, but that may not be true as I am not sure exactly how PHP code is called compared to unity c# with the update/start function. Maybe those answer may assist you – g_l Jun 26 '17 at 12:44

1 Answers1

1

Do this to ensure PHP is handling things as it should:

<?php
if (isset($_REQUEST["var1"])) {
    echo "Received ". $_REQUEST["var1"]. " success!";
    exit();
} else {
    http_status_code(400);
    echo "Request Failed";
}

Remove everything else (I don't think you need it).

For the purpse of verifying http://www.purelogicgames.com/index.php should result in a 400 error and echo "Request Failed" while http://www.purelogicgames.com/index.php?var1=testvalue should result in an OK response and echo "Received testvalue success!".

If this happens then the PHP code is doing what it should.

The C# code does seem to work fine, the log seems to be truncating the received response (which will also be fixed if the response is much shorter like the one above).

apokryfos
  • 38,771
  • 9
  • 70
  • 114
  • Okay I have changed the php code as described and when I put http://www.purelogicgames.com/index.php?var1=testvalue into my browser i see 'Received testvalue success!' but when I put http://www.purelogicgames.com/index.php i get nothing so no 'Request failed'. But nonetheless this doesn't allow me to input a variable in my c# unity code and output it via echo in unity, when i change the web link in c# also i get no change. Hopefully you can make sense of this unlike me @apokryfos – g_l Jun 26 '17 at 20:26
  • @g_l seeing your page I see the `` still, this means that you cannot use `http_status_code` since you're already began sending a response body. remove all other text (even the doctype) and only keep what's here. – apokryfos Jun 26 '17 at 21:13
  • Thanks I'll do that, I presumed it was just part of Netbeans – g_l Jun 27 '17 at 09:35
  • Yep that works but how can I access this variable in the php script and prove i have received 'hello' from the unity script? @apokryfos – g_l Jun 28 '17 at 20:08
  • You could only verify results by either passing the data back to the unity script and verifying it there, or writing a log entry of some sort (basically doing `file_put_contents("/tmp/dump.txt", $_REQUEST["var1"])` the issue is that if you call it from the unity script instead of a browser, then unity will be the receiver of any messages. – apokryfos Jun 28 '17 at 20:23
  • Thanks, I think I have realized the proof in the result text returned by unity which is the echo message from the php script showing it has been received, thanks this is a simple solution which does the desired @apokryfos, I hope that I wasn't too difficult and I wonder where would I have found out how to do this anyway? Maybe by knowing that i can solve some future problems – g_l Jun 29 '17 at 18:54
  • Basically what you're doing here is taking the first basic steps into building a Web API. It may be worth looking up the basics of PHP and how it handles web requests. – apokryfos Jun 29 '17 at 21:28