Tuesday, September 27, 2022
HomeGame Developmentphp - Create token in Unity to ship submit request to Laravel...

php – Create token in Unity to ship submit request to Laravel controller


It’s a mixture of this tutorial on YouTube: Methods to Use UnityWebRequest – Substitute for WWW Class – Unity Tutorial and this reply on an identical query: UnityWebRequest POST to PHP not work

The know-how I’m utilizing:

  • Laravel Framework 7.15.0 (PHP)
  • Unity: 2019.3.11f1 (script is C#)

Now I’m simply sending fundamental types not photos and so forth – so I have not tried something complicated however this sends a get to at least one URL on Laravel which returns the CSRF token.

That is then added as an extra subject to the submit request.

I’m clicking the two buttons fairly rapidly – I might counsel (and can implement this myself later) that you do not ask for the token till your able to submit the shape – to keep away from it expiring in case your kind is kind of lengthy and so forth.

The C# Script (Unity)

Each the features are set as ON Click on() on the buttons in a easy UI Canvas in Unity.

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

public class BasicWebCall : MonoBehaviour
{
    public Textual content messageText;
    public InputField scoreToSend;
    personal string csrf_token;

    readonly string getURL = "http://mazegames.check/leaderboard/1";
    readonly string postURL = "http://mazegames.check/register/1";

    personal void Begin()
    {
        messageText.textual content = "Press buttons to work together with internet server";
    }

    public void OnButtonGetScore()
    {
        messageText.textual content = "Getting Token";
        StartCoroutine(SimpleGetRequest());
    }

    IEnumerator SimpleGetRequest()
    {
        UnityWebRequest www = UnityWebRequest.Get(getURL);

        yield return www.SendWebRequest();

        if(www.isNetworkError || www.isHttpError)
        {
            Debug.LogError(www.error);
        }
        else
        {
            messageText.textual content = "Token Obtained: " + www.downloadHandler.textual content;
            csrf_token = www.downloadHandler.textual content;
        }
    }

    public void OnButtonSendScore()
    {
        if (scoreToSend.textual content == string.Empty)
        {
            messageText.textual content = "Error: No excessive rating to ship.nEnter a worth within the enter subject.";
        }
        else
        {
            messageText.textual content = "Sending Put up Request";
            StartCoroutine(SimplePostRequest(scoreToSend.textual content));
        }
    }

    IEnumerator SimplePostRequest(string curScore)
    {

        Dictionary<string, string> wwwForm = new Dictionary<string, string>();
        wwwForm.Add("_token", csrf_token);
        UnityWebRequest www = UnityWebRequest.Put up(postURL, wwwForm);
        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
        {
            Debug.LogError(www.error);
        }

        else
        {
            messageText.textual content = www.downloadHandler.textual content;
        }
    }
}

Laravel routes/internet.php

Clearly you have to configure your finish factors to obtain your knowledge, validate and retailer it if vital. On this instance its simply validating the token as a part of the submit request.

Route::submit('register/{game_id}', operate($game_id) {
    return "Recreation On!";
});

Route::get('leaderboard/{game_id}', operate($game_id) {
    return csrf_token();
});

And that’s just about it – hope this helps another person.

Request Token on Submission

To solely get the token when your submitting the shape, actually all it’s a must to do is put this line:

StartCoroutine(SimpleGetRequest());

above the road:

StartCoroutine(SimplePostRequest(scoreToSend.textual content));

so that’s appears like this:

StartCoroutine(SimpleGetRequest());
StartCoroutine(SimplePostRequest(scoreToSend.textual content));

Clearly you possibly can then take away the operate SimpleGetRequest altogether.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments