u3d上传json到php 并接收php发送的json
2022/6/11 1:20:21
本文主要是介绍u3d上传json到php 并接收php发送的json,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
U3d代码
class UserInfo { public string id; public string name; public UserInfo() { } public UserInfo(string id,string name) { this.id = id; this.name = name; } } public class TestHtml : MonoBehaviour { string url = "http://127.0.0.1/test.php"; byte[] namebyte; void Awake() {
//1.用这个构造函数的时候。class UserIfo 不能有pulic UserInfo(){}这个空的构造函数
//UserInfo userinfo = new UserInfo("wu","wenbo"); //string nameinfo = JsonMapper.ToJson(userinfo);
2.用这个构造函数的时候。class UserIfo 需要有pulic UserInfo(){}这个空的构造函数
UserInfo userinfo = new UserInfo(); userinfo.id = "wu"; userinfo.name = "wenbo"; string nameinfo = JsonMapper.ToJson(userinfo); StartCoroutine(UnityWebRequestPost(url, nameinfo, text => { Debug.Log(text); })); } } IEnumerator UnityWebRequestPost(string url, string jsonStr, Action<string> textCallBack) { byte[] bytes = Encoding.UTF8.GetBytes(jsonStr); UnityWebRequest request = new UnityWebRequest(url, UnityWebRequest.kHttpVerbPOST) { uploadHandler = new UploadHandlerRaw(bytes), downloadHandler = new DownloadHandlerBuffer() }; request.SetRequestHeader("Content-Type", "application/json;charset=utf-8"); yield return request.SendWebRequest(); if (request.isHttpError || request.isNetworkError) { Debug.LogError(request.error); } else { string result = request.downloadHandler.text; Debug.Log(result); // string ss = System.Text.Encoding.UTF8.GetString(request.downloadHandler.data, 0, request.downloadHandler.data.Length - 0); JsonData objs = JsonMapper.ToObject(request.downloadHandler.text); Debug.Log(objs["id"]); Debug.Log(objs["name"]); Debug.Log(objs.ToJson()); } }
PHP代码
$data = file_get_contents('php://input'); echo $data;
这篇关于u3d上传json到php 并接收php发送的json的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-21Vue3教程:新手入门到实践应用
- 2024-12-21VueRouter4教程:从入门到实践
- 2024-12-20Vue3项目实战:从入门到上手
- 2024-12-20Vue3项目实战:新手入门教程
- 2024-12-20VueRouter4项目实战:新手入门教程
- 2024-12-20如何实现JDBC和jsp的关系?-icode9专业技术文章分享
- 2024-12-20Vue项目中实现TagsView标签栏导航的简单教程
- 2024-12-20Vue3入门教程:从零开始搭建你的第一个Vue3项目
- 2024-12-20从零开始学习vueRouter4:基础教程
- 2024-12-20Vuex4课程:新手入门到上手实战全攻略