Search Unity

Get a variable php-->Unity

Discussion in 'Scripting' started by guel135, Jul 21, 2010.

  1. guel135

    guel135

    Joined:
    Jul 2, 2010
    Posts:
    8
    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.
     
  2. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    Here's one way of doing it:

    Unity side:

    Code (csharp):
    1. WWWForm wwwForm = new WWWForm ();
    2. wwwForm.AddField ("ValueA", "22");
    3. wwwForm.AddField ("ValueB", "44");
    4. WWW www = new WWW ("http://www.my.site/My.php", wwwForm);
    5. yield www;
    6. if (!String.IsNullOrEmpty ([url]www.error[/url]))
    7. {
    8.    throw new System.ApplicationException ("WWW error: " + [url]www.error[/url]);
    9. }
    10. string[] values = [url]www.data.Split[/url] ('\n');
    11. foreach (string value in values)
    12. {
    13.     if (String.IsNullOrEmpty (value.Trim ())
    14.     {
    15.         continue;
    16.     }
    17.     string variable = value.Split ("=");
    18.     Debug.Log ("PHP data: " + variable [0].Trim () + " = " + variable [1].Trim ());
    19. }
    PHP side:

    Code (csharp):
    1. $ValueA = $_POST ["ValueA"];
    2. $ValueB = $_POST ["ValueB"];
    3.  
    4. $ResultA = $ValueA * $ValueB;
    5. $ResultB = $ValueA + $ValueB;
    6.  
    7. echo ("ResultA = $ResultA\n");
    8. 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).
     
  3. guel135

    guel135

    Joined:
    Jul 2, 2010
    Posts:
    8
    hi! many thanks for the reply but or i dont understand or I had explained me very bad(can be both :D )

    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 !!!!
     
  4. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    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:

    http://forum.unity3d.com/viewtopic.php?p=131393
     
    tompongtheo likes this.
  5. eopusDrive

    eopusDrive

    Joined:
    Dec 6, 2013
    Posts:
    4
    @AngryAnt could you repost the last link, its dead and i would love to learn about this method!
     
  6. Thewhiteaura

    Thewhiteaura

    Joined:
    Mar 19, 2015
    Posts:
    7
    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.
    http://answers.unity3d.com/questions/118729/variable-from-php-to-unity.html
    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)
    <?php
    $returnToUnity = $db->lastInsertId();
    print $returnToUnity;

    after that if you grab the same variable in your C# or javascript that you used to post your form and access the text property, the only variable in it should be the one your looking for.
    For example
    lets say you used a WWW form called webReq(save_url, form), you should see the variable in your console with just webReq.text directly after your yield return webReq.

    Hope this helps you and anybody else who is having troubles with this, took me the better part of 11 days to fine tune the database to actually accept variables just to find out they were unsafe, rewrite to use PDO instead, and then finaly add a little extra security as well as a funny qoute should you try to access the database through a browser lol. happy coding everyone!
    ,V,,
    (Note) if you are echoing anything else through this same script, those will show up too, in this case you would have to use string.split which seems messy and can be unstable if you add more overtime, I imagine you would take all those out once your scripts are functioning properly however ;)
     
    Last edited: May 25, 2015