1

I have a unity3d c# script for get data from php file.

Unity C# file:

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 file

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

What I am getting in unity is

Text :
DownLoad Bytes: 110

Any Idea how to get data from php .

Edit c# Code :

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 () {

    }
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
jiten
  • 5,128
  • 4
  • 44
  • 73
  • 1
    Considered making an api - Use JSON responses? – James Lalor Apr 21 '16 at 13:04
  • Don't know why but +1. That code shouldn't compile because `Debug.Log("Text : "www.text);` and `Debug.Log("DownLoad Bytes: "www.bytesDownloaded.ToString());` are missing `+` sign before `www`.Please have compilable code asking questions. – Programmer Apr 21 '16 at 13:08
  • Also judging from the comment you made in one of my answers, I suggest you do `` and make sure that you can get "Hello" from Unity side. Once you verify that you can get Hello from the php then you can go ahead and send the data in JSON format. Also remove the html code from your file. Your php code should start with ``. – Programmer Apr 21 '16 at 13:56
  • @Programmer, Now I an able to see the data in unity GUI, But When I build for webgl .nothing is show on GUI. – jiten Apr 21 '16 at 17:07
  • Is this with the hello world I told you to try? Also is this a real server or a local server? – Programmer Apr 21 '16 at 17:12
  • Yes I tried "Hello world" in php, and It show on Unity but webgl has issue – jiten Apr 21 '16 at 17:28
  • WWW should work on Web Browser. .Dont use OnGUI. Create a UI text from the Editor. `public Text txt;` then store the result to that text with `txt.text= www.text` inlude ` using UnityEngine.UI;` at the top. If that does not work then I will provide answer with UnityWeb instead of WWW. Also what browser is this? – Programmer Apr 21 '16 at 18:18
  • @Programmer, See the edit part, I have checked on firefox and chrome. – jiten Apr 21 '16 at 18:29
  • I told you, don't use OnGUI. Create a text. I updated your last script. Watch the first 22 seconds of this video https://www.youtube.com/watch?v=l8XWCPe3bYU to see how to create a text. Then drag the text to the txt slot in the script. – Programmer Apr 21 '16 at 18:43
  • Thanks @Programmer...Now its run.....instead of text, it is showing Generic/unknow http – jiten Apr 21 '16 at 19:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/109849/discussion-between-programmer-and-vikky). – Programmer Apr 21 '16 at 19:21
  • Join the chat to speed this up. This is getting longer. – Programmer Apr 21 '16 at 19:22
  • i am getting empty reply from server – Muhammad Faizan Khan Nov 03 '16 at 04:59

1 Answers1

2

Ahoy, vikky,

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.

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) 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) to use xml you might try something like this

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

Hope that helps

PS. Decoding JSON in C# might also come in handy

Community
  • 1
  • 1
Hop hop
  • 841
  • 8
  • 21