简体   繁体   中英

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.

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 Log:

WWW Ok!:
: file_get_contents(username=testuser&password=testpass): failed to open stream: No such file or directory in on line :file_get_contents(username = testuser&password = testpass):无法打开流:第行的没有此类文件或目录

: Invalid argument supplied for foreach() in on line :在第行的为foreach()提供的参数无效
JSON data added. UnityEngine.Debug:Log(Object) c__Iterator3:MoveNext() (at Assets/Scripts/JSON.cs:31) UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)

PHP error:

Warning: file_get_contents(): Filename cannot be empty in C:\\xampp\\htdocs\\php\\page.php on line 8

Warning: Invalid argument supplied for foreach() in C:\\xampp\\htdocs\\php\\page.php on line 12 JSON data added.

Edit 1: No error, No data.

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

Edit 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

PHP show:

Notice: Undefined variable: username in C:\\xampp\\htdocs\\php\\page.php on line 5

Notice: Undefined variable: password in C:\\xampp\\htdocs\\php\\page.php on line 7

Edit 3: Final PHP code not add any record to 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. That's not a file path and also not a JSON string. That is just a string. You are not sending a json.

See the log, it is

username=testuser&password=testpass

Use parse_str to get the username and password.

<?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);
}

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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