【问题标题】:Unity Web Request Post not sending dataUnity Web Request Post 不发送数据
【发布时间】:2022-02-04 01:18:10
【问题描述】:

团结 2019.2.1f1

我已经尽可能多地查看了其他解决方案,但似乎没有一个能解决我的问题。

当使用 UnityWebRequest 或 WWW 将 WWWForm 发送到 php 时,表单数据永远不会被读取。

这是我使用 UnityWebRequest 的 c# 代码:

    WWWForm formData = new WWWForm ();
    formData.AddField ("firstname", "firstname");

    UnityWebRequest www = UnityWebRequest.Post (URL, formData);
    //www.chunkedTransfer = false; //<- Tested with this on and off and no difference
    yield return www.SendWebRequest ();

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

这是 PHP 文件的样子:

<?php
    $firstname = $_POST['firstname'];
    echo "HELLO".$firstname."!!!!";
?>

我也尝试过使用旧的万维网方式:

    WWWForm formData = new WWWForm ();
    formData.AddField ("firstname", "firstname");
    WWW www = new WWW("test.php",formData);
    yield return www;
    Debug.Log (www.text);

我从所有这些中得到的输出是:“HELLO!!!!”,而不是“HELLOfirstname!!!”。

我已经通过从 HTML 文件发送一个表单来测试 PHP 代码,它运行良好。

【问题讨论】:

  • 在您的 PHP 上尝试print_r($_REQUEST);
  • WWWForm 是一个类。您需要代码从类中提取文本字符串并放入您的帖子中。
  • @BrettGregson 打印出一个空数组:“Array{}”。
  • 你能在你的php端添加这个字符串吗:var_dump(file_get_contents("php://input"));
  • @myxaxa 这是它返回的内容:string(0) "" HELLO!!!!

标签: c# php unity3d


【解决方案1】:

我复制了您的代码并执行了以下操作:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class pp : MonoBehaviour
{

    bool runReuqest = true;
    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        if (runReuqest)
            StartCoroutine("runRquest");
    }

    IEnumerator runRquest() {
        runReuqest = false;
        // Your Code no changes
        WWWForm formData = new WWWForm();
        formData.AddField("firstname", "firstname");

        UnityWebRequest www = UnityWebRequest.Post("http://my-ip/j.php", formData);
        //www.chunkedTransfer = false; //<- Tested with this on and off and no difference
        yield return www.SendWebRequest();

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

我得到的日志如下:

Log:
Form upload complete!
UnityEngine.Debug:Log(Object)

HELLOfirstname!!!!
UnityEngine.Debug:Log(Object)

检查您的代码是否在某个地方出现问题,或使用以上内容作为指导。

编辑 1 我没有检查以前的答案,但它似乎也对我有用。 j.php: 收到:HELLOfirstname!!!!

你可能还漏掉了一些东西。

【讨论】:

    【解决方案2】:

    我曾经或曾经遇到过同样的问题,所以我使用以下方法: 这里的想法是发送一个名为 POST 的字段,然后让 php 脚本检查 POST,使用 isset($_POST["POST"])

    WWWForm form = new WWWForm();
    form.AddField("post","POST");  // Added line to check for post
    form.AddField("userID", userID);
    

    然后,在 php 脚本中,我执行以下操作:

    if (isset($_POST["POST"]) || (isset($_POST["post"])))
    {
        echo("You sent a POST.\nPrinting the POST...\n");
        print_r($_POST);
        
        $userID = $_POST["userID"]; 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-29
      • 2017-12-11
      • 2013-08-24
      • 2020-07-08
      • 2020-02-01
      • 2019-06-23
      • 2012-02-05
      • 2012-09-02
      相关资源
      最近更新 更多