从PHP获取数据到C#脚本 [英] Get data from php into c# script
问题描述
我有一个unity3d c#脚本,用于从php文件中获取数据。
I have a unity3d c# script for get data from php file.
Unity 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获取数据。
Any Idea how to get data from 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 () {
}
}
推荐答案
Ahoy,vikky,
Ahoy, vikky,
1)我对C#不太了解,但是我要帮助C#开发人员的工作就是以适当的格式向他发送数据。例如,XML或JSON,而不是表。
1) I am not very experienced with C# but what I would do to help a C# developer is to send him the data in appropriate format. For example XML or JSON, not tables.
所以我会尝试使用JSON:
so I'd try this for 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>< / tr>,$ myrow ['pk']);
代替 printf(< tr>< td>%s< / td>,$ myrow ['pk'])的;
2) if you insist to have a table use the correct HTML for table
use printf ("<tr><td>%s</td></tr>", $myrow['pk']);
instead of printf ("<tr><td>%s</td>", $myrow['pk']);
3)使用xml,您可以尝试一些操作像这样
3) to use xml you might try something like this
4),您可以使用来自C#的远程连接(如果您可以选择)直接连接到Postgre数据库此
4) you could connect directly to your Postgre database using a remote connection from C# (if that is an option for you) something along the lines of this
希望有帮助
PS。 在C#中解码JSON 也可能会派上用场
PS. Decoding JSON in C# might also come in handy
这篇关于从PHP获取数据到C#脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!