Hi! how can i read variable from php to unity? Because i can write to sql from unity and read sql from unity but i want to have a $var in php . and later read it in unity.
sorry for my english for me is very hard to explain it.
Hi! how can i read variable from php to unity? Because i can write to sql from unity and read sql from unity but i want to have a $var in php . and later read it in unity.
sorry for my english for me is very hard to explain it.
Hereâs one way of doing it:
Unity side:
WWWForm wwwForm = new WWWForm ();
wwwForm.AddField ("ValueA", "22");
wwwForm.AddField ("ValueB", "44");
WWW www = new WWW ("http://www.my.site/My.php", wwwForm);
yield www;
if (!String.IsNullOrEmpty ([url]www.error[/url]))
{
throw new System.ApplicationException ("WWW error: " + [url]www.error[/url]);
}
string[] values = [url]www.data.Split[/url] ('\n');
foreach (string value in values)
{
if (String.IsNullOrEmpty (value.Trim ())
{
continue;
}
string variable = value.Split ("=");
Debug.Log ("PHP data: " + variable [0].Trim () + " = " + variable [1].Trim ());
}
PHP side:
$ValueA = $_POST ["ValueA"];
$ValueB = $_POST ["ValueB"];
$ResultA = $ValueA * $ValueB;
$ResultB = $ValueA + $ValueB;
echo ("ResultA = $ResultA\n");
echo ("ResultB = $ResultB\n");
Notice that you donât need to provide and parse PHP variable names. If youâre absolutely certain about the order of the data being transmitted, you could also just read values based on that (most commonly this is what people do).
hi! many thanks for the reply but or i dont understand or I had explained me very bad(can be both :lol: )
full explain that i want to do:
i have a variable $user in my php file and i want to take this variable inside the unity webplayer. For work with the unity variable inside with java.(i cant use the get url method because the php call to another php and later load another php )
sorry, It is my first week working with unity and I am very very slow at the moment. My I want to improve!!
thx !!!
Ah. The code I pasted above is for exchanging data between an already running webplayer and a PHP page. If youâre looking for sending a variable from a PHP file to a webplayer run by that PHP file, this would be what you are looking for:
@AngryAnt could you repost the last link, its dead and i would love to learn about this method!
Hey eopus, I have the same question, and upon further investigation, I think i found it. I canât believe i didnt think of it, as im using something like this already to display other variables haha⌠I guess I thought there would be a simpler way to do it.
I forgot to add, if your using C# you would use WWW = new WWW(âetcâ) and would have to wrap the entire function into an enumerator in order to use the yield command.
EDIT - Now that i have a final working version with this that gets a variable from within the same script that writes over to php⌠if you already have a script setup to save variables to a database using the WWW or WWWForm, all you need to do is this: (right after your execute,
or query(highly recommended to use prepared statements)