繁体   English   中英

PHP阅读Unity C#问题的JSON帖子

[英]PHP reading JSON post from Unity C# issue

我们试图将简单的JSON发送到在XAMP上运行的本地php并将数据保存到MySql,我们尝试了许多不同的代码我们认为Unity C#代码是真的但我们不确定,我们测试了许多不同的PHP代码,但是半代码没有显示半个显示一些错误,我发布了最后使用的代码,任何建议欢迎。

C#Unity:

using UnityEngine;
using System.Collections;
using System.Text;


public class JSON : MonoBehaviour 
{


IEnumerator Start() 
{
Debug.Log ("Posting JSON...");

string URL = "http://localhost/php/page.php";

WWWForm form = new WWWForm ();
form.AddField ("username", "testuser");
form.AddField ("password", "testpass");

var headers = form.headers;
headers["content-type"] = "application/json";

WWW www = new WWW(URL, form.data, headers);

yield return www;
Debug.Log (www.uploadProgress);

if (www.error == null)
 {
    Debug.Log("WWW Ok!: " + www.text);
 } else {
    Debug.Log("WWW Error: "+ www.error);
 }    

  }

}

PHP XAMP:

<?php


$connect = mysqli_connect("localhost","root","", "localdb");

$filename = file_get_contents('php://input');

$data = file_get_contents($filename);

$array = json_decode($data, true);

foreach ($array as $row)
{
$sql = "INSERT INTO localtable (username, password)   VALUES('".$row[username]."','".$row[password]."')";       
mysqli_query($connect, $sql);
}

echo $data;

?>

Unity日志:

WWW好的!:
警告 :file_get_contents(username = testuser&password = testpass):无法打开流:第8行的C:\\ xampp \\ htdocs \\ php \\ page.php中没有此类文件或目录

警告 :在第12行的C:\\ xampp \\ htdocs \\ php \\ page.php中为foreach()提供的参数无效
添加了JSON数据。 UnityEngine.Debug:Log(Object)c__Iterator3:MoveNext()(在Assets / Scripts / JSON.cs:31)UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator,IntPtr)

PHP错误:

警告:file_get_contents():第8行的C:\\ xampp \\ htdocs \\ php \\ page.php中的文件名不能为空

警告:在第12行添加的JSON数据的C:\\ xampp \\ htdocs \\ php \\ page.php中为foreach()提供的参数无效。

编辑1:没有错误,没有数据。

<?php
 $data = json_decode(file_get_contents('php://input'), true);
 print_r($data);
 echo $data["username"];
 echo $data["password"];
?>

编辑2:

 <?php
   $data = file_get_contents('php://input');// $data == "username=testuser&password=testpass"
   parse_str($data, $result);
   echo $result["username"]; // "testuser"
   echo "   ";
   echo $result["password"]; // "testpass"
   echo "   ";
?>

Unity显示数据:testuser testpass

PHP显示:

注意:未定义的变量:第5行的C:\\ xampp \\ htdocs \\ php \\ page.php中的用户名

注意:未定义的变量:第7行的C:\\ xampp \\ htdocs \\ php \\ page.php中的密码

编辑3:最终的PHP代码不向Mysql添加任何记录

    <?php
   $data = file_get_contents('php://input');// $data == "username=testuser&password=testpass"
   parse_str($data, $result);
   echo $result["username"]; // "testuser"
   echo "   ";
   echo $result["password"]; // "testpass"
   echo "   ";

   $connect = mysqli_connect("localhost","admin","123456", "localdb");
   $array = json_decode($data, true);

   $sql = "INSERT INTO localtable (username, password)   VALUES('".$result["password"]."','".$result["password"]."')";       
   mysqli_query($connect, $sql);

   echo "   ";

   echo $queryResult = mysqli_query($connect, $sql);//Return 1
?>
$filename = file_get_contents('php://input');

$data = file_get_contents($filename);

$filename是您从Unity3D客户端获取的表单数据。 这不是文件路径,也不是JSON字符串。 那只是一个字符串。 没有发送一个json。

查看日志,它是

username=testuser&password=testpass

使用parse_str获取用户名和密码。

<?php
$connect = mysqli_connect("localhost","root","", "localdb");
$data = file_get_contents('php://input');// $data == "username=testuser&password=testpass"
$result = null;
parse_str($data, $result);    
echo $result["username"]; // "testuser"
echo $result["password"]; // "testpass"

if ($stmt = $mysqli_prepare("INSERT INTO localtable (username, password) VALUES(?,?)")) {
    /* bind parameters for markers */
    mysqli_stmt_bind_param($stmt, "username", $result["username"]);
    mysqli_stmt_bind_param($stmt, "password", $result["password"]);

    /* execute query */
    mysqli_stmt_execute($stmt);

    /* close statement */
    mysqli_stmt_close($stmt);
}

?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM