2016-04-21 116 views
0

我有一個用於從php文件獲取數據的unity3d c#腳本。從php獲取數據到c#腳本

統一C#文件:

using UnityEngine; 
using System.Collections; 

public class FrmPhp : MonoBehaviour { 

    // Use this for initialization 
    void Start() { 
     string url = "http://localhost/abc/PgComm.php"; 
     WWW www = new WWW(url); 
     StartCoroutine(WaitForRequest(www)); 
    } 
    IEnumerator WaitForRequest(WWW www) 
    { 
     yield return www; 
     // check for errors 
     if (www.error == null) 
     { 
      Debug.Log("Text : "www.text); 
      Debug.Log("DownLoad Bytes: "www.bytesDownloaded.ToString()); 
     } else { 
      Debug.Log("WWW Error: "+ www.error); 
     }  
    } 

    // Update is called once per frame 
    void Update() { 

    } 
} 

PHP文件

<html> 
    <body> 
     <table name="mytable" border="0" cellspacing="0" cellpadding="0"> 
      <tr> 
       <td> 
        Friend ID 
       </td> 

      </tr> 
     <?php 
     $db = pg_connect('host=localhost dbname=MyDB user=postgres password=xyz'); 

     $query = "SELECT pk FROM Table1"; 

     $result = pg_query($query); 
     //printf ("<tr><td>%s</td>", $result); 

     if (!$result) { 
      echo "Problem with query " . $query . "<br/>"; 
      echo pg_last_error(); 
      exit(); 
     } 

     while($myrow = pg_fetch_assoc($result)) { 
      printf ("<tr><td>%s</td>", $myrow['pk']); 

     } 
     ?> 
     </table> 
    </body> 
</html> 

什麼我越來越團結是

Text : 
DownLoad Bytes: 110 

任何想法如何擺脫PHP數據。

編輯C#代碼:

using UnityEngine; 
using System.Collections; 
using System.Collections.Generic; 
using UnityEngine.Experimental.Networking; 

public class FrmPhp : MonoBehaviour { 
    public Text txt; 
    string url = "http://192.100.233.222/abc/Hello.php"; 
    byte[] results; 
    //byte results1 = 0x00; 
    // Use this for initialization 
    void Start() { 
     StartCoroutine(GetText()); 
    } 

    IEnumerator GetText() 
    { 
     UnityWebRequest www = UnityWebRequest.Get(url); 
     yield return www.Send(); 

     if(www.isError) { 
      Debug.Log(www.error); 
      txt.text = www.error; 
     } 
     else { 
      // Show results as text 
      //results1 = 0x01; 
      Debug.Log(www.downloadHandler.text); 
      txt.text = www.downloadHandler.text; 
     } 
    } 
    // Update is called once per frame 
    void Update() { 

    } 
} 
+0

爲什麼downvote .... – vikky

+1

考慮製作api - 使用JSON響應? –

+0

不知道爲什麼,但+1。該代碼不應該編譯,因爲'Debug.Log(「Text:」www.text);''和'Debug.Log(「DownLoad Bytes:」www.bytesDownloaded.ToString());'在之前缺少'+'符號'www'.Please有可編譯的代碼問問題。 – Programmer

回答

1

嗨,vikky,

1)我不是很有經驗的C#,但我會做什麼來幫助C#開發人員是送他的數據以適當的格式。例如XML或JSON,而不是表格。

,所以我想嘗試一下本作JSON:

<?php 
$db = pg_connect('host=localhost dbname=MyDB user=postgres password=xyz'); 
$query = "SELECT pk FROM Table1"; 
$result = pg_query($query); 
//printf ("<tr><td>%s</td>", $result); 
if (!$result) { 
    echo "Problem with query " . $query . "<br/>"; 
    echo pg_last_error(); 
    exit(); 
} 
$return_arr = array(); 
while($myrow = pg_fetch_assoc($result)) { 
    array_push($return_arr, $myrow); 
} 
echo json_encode($return_arr); 

2)如果你堅持有一個表使用正確的HTML表格 代替printf ("<tr><td>%s</td>", $myrow['pk']);

3使用printf ("<tr><td>%s</td></tr>", $myrow['pk']); )使用xml你可能try something like this

4)你可以直接連接到你的Postgre數據庫使用從C#遠程連接(如果這是一個選項)沿lin this

希望幫助

PS的ES。 Decoding JSON in C#也可能派上用場