【问题标题】:Unity3D - Post is empty when doing a UnityWebRequestUnity3D - 执行 UnityWebRequest 时帖子为空
【发布时间】:2019-10-21 12:31:24
【问题描述】:

我正在执行 UnityWebrequest,它工作正常,但是在检查我的 PHP 中的帖子是什么后,我发现帖子是一个空数组并且文件也是如此。我发现了一个有类似问题的 StackOverflow 帖子。但它的解决方案对我不起作用。那么为什么帖子和文件是空的呢?我在本地服务器上运行它。

UnityWebRequest POST to PHP not work

网络请求

static IEnumerator Post(string url, string bodyJsonString)
{
    UnityWebRequest request = new UnityWebRequest(url, "POST");
    request.chunkedTransfer = false;
    Debug.Log(bodyJsonString);
    byte[] bodyRaw = Encoding.UTF8.GetBytes(bodyJsonString);
    request.uploadHandler = new UploadHandlerRaw(bodyRaw);
    Debug.Log(request.uploadHandler.data.Length);
    request.downloadHandler = new DownloadHandlerBuffer();
    request.SetRequestHeader("Content-Type", "application/json");
    yield return request.SendWebRequest();
    Debug.Log("Status Code: " + request.responseCode);
    Debug.Log(request.downloadHandler.text);
}

PHP

<?php
echo "POST: ";
print_r($_POST);
var_dump($_POST);

echo "Files: ";
print_r($_FILES);
var_dump($_FILES);

调试

{"testJson":1}

8

状态码:200

POST:数组()数组(0){}文件:数组()数组(0){}

【问题讨论】:

    标签: php unity3d


    【解决方案1】:

    我遇到了同样的问题,这对我有用。

    之后

    UnityWebRequest request = new UnityWebRequest(url, "POST");
    

    添加

    request.chunkedTransfer = false;
    

    让我难过很久的事情是我需要在 url 中包含文件名。我使用的是 index.php,所以我只是使用 url 的目录路径。当我使用包含文件名的路径后,它就开始正常工作了。

    【讨论】:

      【解决方案2】:

      当我通过 WWWForm 添加内容时,问题就解决了。然后填充了 $_Post 数组。

      https://docs.unity3d.com/ScriptReference/WWWForm.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-29
        • 1970-01-01
        • 2014-08-23
        相关资源
        最近更新 更多