从 unity 发送请求时 file_get_contents 返回 null

2024-01-01

我正在 PHP 中处理 post 请求数据:

header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");


    $data = json_decode(file_get_contents("php://input"));

    print $data;

    if(empty($data)){
        echo "\n data is empty";
    }

并从 C# 发送数据:

string postData = JsonUtility.ToJson(correction);
        Debug.Log(postData);
        using (UnityWebRequest www = UnityWebRequest.Post(CorrectionMarkerAPIPaths.pathCorrectionCreate, postData))
        {
            www.SetRequestHeader("Content-Type", "application/json; charset=UTF-8");
            yield return www.Send();
            if (www.isError)
            {
                Debug.Log("Correction create failed with error : " + www.error);
            }
            else
            {
                Debug.Log("Correction created sucesful with message"+ www.downloadHandler.text);
            }
        }

但在服务器端,我收到错误$data是空的


对于所有提出这个问题的人:

从我们的谈话来看。unity发送请求为urlencoded. The UnityWebRequest.Post http://Form%20body%20data.%20Will%20be%20URLEncoded%20prior%20to%20transmission.发送 urlencoded 数据:

  1. uri表单数据将传输到的目标 URI。

  2. postData形成身体数据。在传输之前将进行 URLEncoded。

解码数据PHP.

$request = file_get_contents("php://input");
$decoded = urldecode($request);

将 json 解析为对象:

$data = json_decode($decoded);

获取json的属性。

$data->{attribute};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 unity 发送请求时 file_get_contents 返回 null 的相关文章

随机推荐