Sending variable value to php from Unity

Viewed 127

I have a php file which is used to display a pdf file. The pdf file contains variables obtained from php file given below.

$areaOfCircle;
...
...
$pdf->WriteFixedPosHTML($areaOfCircle, 20, 50, 20, 50, 'auto');

And I have my Unity script here

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

public class SendToServer: MonoBehaviour
{
    public string areOfCircle;
   
    IEnumerator Start()
    {
        WWWForm form = new WWWForm();
        form.AddField("areaOfCircle", areOfCircle);
        UnityWebRequest www = UnityWebRequest.Get("https://localhost/sqlconnect/myFirstPhp.php");
        yield return www.SendWebRequest();
        if (www.isNetworkError || www.isHttpError)
        {
            Debug.LogError(www.error);
        }
        else
        {
            Debug.Log("Sent");
        }
    }
}

I am trying to send value of the string to php from Unity but I only get error message. What am I doing wrong here?

2 Answers

Well, you are not sending your form or any data at all.

If you want to use POST then it should rather be UnityWebRequest.Post

UnityWebRequest www = UnityWebRequest.Post("https://localhost/sqlconnect/myFirstPhp.php", form);

If your PHP however actually expects GET then you would probably rather do

UnityWebRequest www = UnityWebRequest.Get($"https://localhost/sqlconnect/myFirstPhp.php?areaOfCircle={areOfCircle}&someSecondParameter{secondParameter}");

Don't see unfortunately how exactly you are setting the $areaOfCircle variable in your PHP code ...

You can add more variables in the url separated by the & char

UnityWebRequest.Get($"https://localhost/sqlconnect/myFirstPhp.php?areaOfCircle={areaOfCircle}&secondVariable={secondValue}");

In PHP you get them with

$areaOfCirle = $_GET["areaOfCircle"];
$secondVariable = $_GET["secondVariable"];
Related