簡體   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