简体   繁体   English

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

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

We are trying to send simple JSON to local php runing on XAMP and save data to MySql, we tried many different codes we think Unity C# code is true but we are not sure, we tested many different PHP codes but half codes show nothing half show some error, i post both last used codes, any suggest welcome. 我们试图将简单的JSON发送到在XAMP上运行的本地php并将数据保存到MySql,我们尝试了许多不同的代码我们认为Unity C#代码是真的但我们不确定,我们测试了许多不同的PHP代码,但是半代码没有显示半个显示一些错误,我发布了最后使用的代码,任何建议欢迎。

C# Unity: 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 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 Log: Unity日志:

WWW Ok!: WWW好的!:
Warning : file_get_contents(username=testuser&password=testpass): failed to open stream: No such file or directory in C:\\xampp\\htdocs\\php\\page.php on line 8 警告 :file_get_contents(username = testuser&password = testpass):无法打开流:第8行的C:\\ xampp \\ htdocs \\ php \\ page.php中没有此类文件或目录

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

PHP error: PHP错误:

Warning: file_get_contents(): Filename cannot be empty in C:\\xampp\\htdocs\\php\\page.php on line 8 警告:file_get_contents():第8行的C:\\ xampp \\ htdocs \\ php \\ page.php中的文件名不能为空

Warning: Invalid argument supplied for foreach() in C:\\xampp\\htdocs\\php\\page.php on line 12 JSON data added. 警告:在第12行添加的JSON数据的C:\\ xampp \\ htdocs \\ php \\ page.php中为foreach()提供的参数无效。

Edit 1: No error, No data. 编辑1:没有错误,没有数据。

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

Edit 2: 编辑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 show data: testuser testpass Unity显示数据:testuser testpass

PHP show: PHP显示:

Notice: Undefined variable: username in C:\\xampp\\htdocs\\php\\page.php on line 5 注意:未定义的变量:第5行的C:\\ xampp \\ htdocs \\ php \\ page.php中的用户名

Notice: Undefined variable: password in C:\\xampp\\htdocs\\php\\page.php on line 7 注意:未定义的变量:第7行的C:\\ xampp \\ htdocs \\ php \\ page.php中的密码

Edit 3: Final PHP code not add any record to Mysql 编辑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 is the form data you got from the Unity3D client. $filename是您从Unity3D客户端获取的表单数据。 That's not a file path and also not a JSON string. 这不是文件路径,也不是JSON字符串。 That is just a string. 那只是一个字符串。 You are not sending a json. 没有发送一个json。

See the log, it is 查看日志,它是

username=testuser&password=testpass

Use parse_str to get the username and password. 使用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