2016-04-26 65 views
0

我有一個場景,我必須從php獲取數據到我的Unity3d c#腳本中。當我調用www.text時,我得到整個html頁面代碼。在PHP頁面中,我只是echo的數據。www.Text返回整個html頁面不是數據

void Start() { 
     StartCoroutine(GetText()); 
    } 

    IEnumerator GetText() { 
     UnityWebRequest www = new UnityWebRequest("http://192.18.23.1/php/Time.php"); 
     www.downloadHandler = new DownloadHandlerBuffer(); 
     yield return www.Send(); 

     if(www.isError) { 
      Debug.Log(www.error); 
     } 
     else { 
      // Show results as text 
      //Debug.Log(www.downloadHandler.text); 
      // Or retrieve results as binary data 
      byte[] results = www.downloadHandler.data; 
      txt.text = "Success # " + www.downloadHandler.text; 
     } 
    } 

編輯:

Time.php準則

<html> 
<head> 
<script src="jquery.min.js" type="text/javascript"></script> 
    <script src="mqttws31.js" type="text/javascript"></script> 


    <script> 
    function myFunction(p1, p2) { 
    return p1 * p2; 
    }; 
    var mqtt,payload; 
    var value = 10; 
    var reconnectTimeout = 2000; 
    function MQTTconnect() { 
    if (typeof path == "undefined") { 
     path = '/mqtt'; 
    } 
    mqtt = new Paho.MQTT.Client(
      'broker', 
      1883, 
      "/mqtt", 
      "a:" + "abcdef" + ":" + Date.now() 
    ); 
     var options = { 
      timeout: 3, 
      useSSL: false, 
      cleanSession: true, 
      onSuccess: onConnect, 
      onFailure: function (message) { 
       $('#status').val("Connection failed: " + message.errorMessage + "Retrying"); 
       setTimeout(MQTTconnect, reconnectTimeout); 
      } 
     }; 

     mqtt.onConnectionLost = onConnectionLost; 
     mqtt.onMessageArrived = onMessageArrived; 

     //if (username != null) { 
      options.userName = 'username'; 
      options.password = 'password'; 
     //} 

     mqtt.connect(options); 

    } 

    function onConnect() { 


     // Connection succeeded; subscribe to our topic 
     mqtt.subscribe('iot-2/type/+/id/+/evt', {qos: 0}); 
     $('#topic').val('iot-2/type/" + "+" + "/id/" + "+" + "/evt'); 

    } 

    function onConnectionLost(response) { 
     setTimeout(MQTTconnect, reconnectTimeout); 
     $('#status').val("connection lost: " + responseObject.errorMessage + ". Reconnecting"); 

    }; 

    function onMessageArrived(message) { 

     var topic = message.destinationName; 
     payload = message.payloadString;   
     //document.getElementById("ws").value = payload;  
    }; 
    </script> 
</head> 
    <body> 
     <?php 
     echo '<script type="text/javascript">document.write(MQTTconnect());</script>';; 
     $ff = $_GET['payload']; 
     echo $ff; 
     ?> 
    </body> 
</html> 
+0

份額php文件,也許這將有助於http://wiki.unity3d.com/index.php?title=Server_Side_Highscores –

+0

@MohammadFaizanKhan查看編輯部分.... – vikky

+0

這是HTML文件,請參閱我以前分享你的鏈接。使單獨的PHP –

回答

0

也許最好的方法是使用服務器端PHP代碼生成輸出,在所有不包括html/javascript代碼。

此外,您還需要刪除任何html標記並將內容類型用作文本以確保解決方案正常工作。

例如:

<?php  
    header("Content-Type: text/plain"); 
    echo 'result'; 
?> 
+0

你能告訴我如何生成輸出,不包括所有的html \ js。 – vikky

相關問題