Unable to use php with UnityWebRequest on AWS S3 server.

I can’t wrap my head around using UnityWebRequest to call .php files in my database.

I have created an AWS S3 bucket and uploaded 2 files:
hello.php

<?php $text = file_get_contents('./hello.txt'); echo $text; ?>

hello.txt
hi there

I am able to access the .php file using this code:

    private void Start()
    {
        StartCoroutine(Get(URL));
    }

    private IEnumerator Get(string URL)
    {
        UnityWebRequest _request = UnityWebRequest.Get(URL);

        yield return _request.SendWebRequest();

        if (_request.isNetworkError || _request.isHttpError)
            Debug.LogError(_request.error);
        else
        {
            string _text = _request.downloadHandler.text;
            print(_text);
        }
    }

But the log message that is being printed to the console looks like this:
6703414--769813--Screenshot 2021-01-08 160427.png

The entire text inside the .php file is being returned.

I tried changing the .php to this:

<?php
echo "hi there";
?>

But the result is exactly the same.

Does anyone know what I’m doing wrong?

If you are expecting that php code to be executed, this has nothing to do with Unity and everything to do with the server it is hosted on. Unity certainly does not execute php!

Otherwise, php is just a text file and Unity is dutifully fetching it and giving it to you.

2 Likes

AHAAAA!
You are right, S3 doesn’t execute php!
I found this to confirm.&text=If%20you%20want%20to%20host,run%20your%20PHP%20application%20easily.)

omg I can finally move on, thank you

1 Like