欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Unity + PHP 上传字符串

程序员文章站 2022-07-13 21:57:14
...

PHP目录结构:

Unity + PHP 上传字符串

php文件: upload_string.php 

</style>
</head>
<body> 
 
<?php 
    if(isset($_POST["id"]))
    {
        $myfile = fopen("newfile.txt", "a") or die("Unable to open file!");
		$txt = $_POST["id"];
		fwrite($myfile, $txt ."\n");
		fclose($myfile);
		fflush($myfile);
        echo "post请求成功,id值为:".$_POST["id"].",cid值为:".$_POST["cid"];
    }    
?>

</body>
</html>

unity 文件:

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

public class UploadString : MonoBehaviour
{
    string url = "http://localhost:8888/upload_string/upload_string.php";
    void OnGUI()
    {
        if (GUILayout.Button("Post php"))
        {
            StartCoroutine(OnGet());
        }
    }

    IEnumerator OnGet()
    {
        //表单
        WWWForm form = new WWWForm();
        form.AddField("id", 1);

        WWW www = new WWW(url, form);

        yield return www;

        if (www.error != null)
        {
            print("php请求错误: 代码为" + www.error);
        }
        else
        {
            print("php请求成功" + www.text);
        }
    }
}

运行程序,点击 post php 按钮 

Unity + PHP 上传字符串 

结果如下:

Unity + PHP 上传字符串