本文介绍了UnityWebRequest POST到PHP不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用带有WWWForm的文档: https://docs.unity3d.com/Manual /UnityWebRequest-SendingForm.html

I use documentation with WWWForm: https://docs.unity3d.com/Manual/UnityWebRequest-SendingForm.html

C#:

void Start() {
    StartCoroutine(Upload());
}

IEnumerator Upload(){
    yield return Upload1();
    yield return Upload2();
}

IEnumerator Upload1() {
    List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
    formData.Add( new MultipartFormDataSection("field1=foo&field2=bar") );
    //formData.Add( new MultipartFormFileSection("my file data", "myfile.txt") );

    UnityWebRequest www = UnityWebRequest.Post(url, formData);
    yield return www.SendWebRequest();

    if(www.isNetworkError || www.isHttpError) {
        Debug.Log(www.error);
    }
    else {
        Debug.Log("Form upload complete!");
        Debug.Log( "MULTIPART: " + www.downloadHandler.text );
    }
}

IEnumerator Upload2() {
    WWWForm form = new WWWForm();
    form.AddField("myField", "myData");

    UnityWebRequest www = UnityWebRequest.Post(url, form);
    yield return www.SendWebRequest();

    if(www.isNetworkError || www.isHttpError) {
        Debug.Log(www.error);
    }
    else {
        Debug.Log("Form upload complete!");
        Debug.Log( "WWWForm: " + www.downloadHandler.text );
    }
}

PHP:

<?php

    echo "POST: ";
    print_r( $_POST );
    var_dump( $_POST );

    echo "GET: ";
    print_r( $_GET );
    var_dump( $_GET );

?>

响应(MULTIPART和WWWForm):

Response (MULTIPART and WWWForm):

POST: Array
(
)
array(0) {
}
GET: Array
(
)
array(0) {
}

$ _ POST为空...(这是我的问题)

使用Unity 2017.3.

$_POST is empty... (that's my problem)

Use Unity 2017.3.

我以前使用过UnityWebRequest.Send(Unity 5.6),它对我一直有效,直到过时.

I used UnityWebRequest.Send (Unity 5.6) before and it worked for me until it became obsolete.

谢谢.

推荐答案

这似乎对我有用,希望对您有所帮助.

This seems to work for me, I hope it helps.

UnityWebRequest www = UnityWebRequest.Post(url, formData);

www.chunkedTransfer = false;////ADD THIS LINE

yield return www.SendWebRequest();

这篇关于UnityWebRequest POST到PHP不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 23:22