The question is answered,
consume a php webservice in Unity3D C#
Hello,
i'm trying to create a simple php HelloWorld webservice and consume it in unity 3D.
here's my webservice script test.php
<?php
function hello() {
return "hello world";
}
try
{
$server = new SoapServer(null, array('uri' => 'http://127.0.0.1/KitchenAR/test.php'));
$server->addFunction('hello');
$server->handle();
}
catch(Exception $e)
{
echo "Exception: " . $e;
}
?>
i've made another php file (code is right bellow) so i can use it my localhost and everything works just fine,
<?php
try
{
$clientSOAP = new SoapClient( null,
array (
'uri' => 'http://127.0.0.1/KitchenAR/test.php',
'location' => 'http://127.0.0.1/KitchenAR/test.php',
'trace' => 1,
'exceptions' => 0
));
$ret = $clientSOAP->__call('hello', array());
echo $ret;
echo '<br />';
}
catch(SoapFault $f)
{
echo $f;
}
?>
moving on to unity i've used the WWW class, but it seems like my www.text is empty.
here's my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Webservices : MonoBehaviour {
public GameObject text;
public string url = "http://127.0.0.1/KitchenAR/test.php";
private Text m_text;
IEnumerator Start()
{
m_text = text.GetComponent<Text>();
WWW www = new WWW(url);
yield return www;
if (www.error == null)
{
text.GetComponent<Text>().text = www.text;
Debug.Log("Sucess: " + text.GetComponent<Text>().text);
}
else
{
Debug.Log("Fail: " + www.error);
}
}
}
any idea what i'm doing wrong ? thanks
Answer by Jon-at-Kaio · Aug 01, 2017 at 01:11 PM
I'm not familiar with the soapservice class you're using there, but I'd hazard a guess that it's expecting a post. And from what I recall www sends a get by default.
I'm on my phone at the moment so can't double check this but I suspect you'll have some luck reading through this forum post
https://forum.unity3d.com/threads/www-wwwform-to-post-json-data-to-server-solved.57490/
Answer by Besbes_Salma · Aug 02, 2017 at 10:19 AM
Okay So i found the solution and it's sooo silly,
the public string url variable was causing the problem ( public string url = "http://127.0.0.1/KitchenAR/test.php"; )
first i set the variable to the wrong url (test.php instead of my second php script wstest.php) and since the variable is public and i'm not initializing it in the start function, the url didn't change to the right one in the unity inspector.
So i just change it to private and the problem was Solved.