#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

    Get variable from xml


    Hi there I have little problem to get an object from an xml output

    I would like to optain a user id from XML so I can continue make an order in WHMCS from the user ID.

    The way to get the responce I have:
    $arr = json_decode($jsondata); # Decode JSON String

    print_r($arr); # Output XML Response as Array

    responce:
    stdClass Object ( [result] => success [clientid] => 7 )

    So my problem is how to get 7 in to $user_id
  2. #2
  3. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,540
    Rep Power
    2337
    $arr is an object.
    clientid is a member variable of that object.
    You access members of an object with ->.

    Code:
    $var = $obj->memberVariable;
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,922
    Rep Power
    1045
    Hi,

    by the way, JSON is not XML. Those are two completely different languages/formats.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0
    So
    $var = $result->result; will give responce success if is success
    $var = $user_id->clientid; will give the user id??
  8. #5
  9. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,540
    Rep Power
    2337
    Try it and see! You're not going to break anything!

    You have a single object with TWO member variables: result and client_id.
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,922
    Rep Power
    1045
    What is $result? What is $user_id? Those aren't defined anywhere in your code.

    When you call json_decode($jsondata), you get back a PHP object representing the JSON document, right? You've stored this object in a variable called $arr (which is a rather unfortunate name for an object).

    If you want the client ID from this object, you use the standard object syntax:

    PHP Code:
    $user_id $arr->clientid
    Maybe it's a good idea to convert the JSON document into an array rather than a PHP object:

    PHP Code:
    <?php

    $jsondata 
    '{"result": "success", "clientid": 7}';

    // parse JSON document and turn it into an associative array
    $response json_decode($jsondatatrue);

    // that's your array
    print_r($response);

    // get user ID
    $user_id $response['clientid'];
    print_r($user_id);
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

    Not working :)


    Originally Posted by ptr2void
    Try it and see! You're not going to break anything!
    Offcource I also tried and tried
    different way cant get it to optain the id
    $var = $obj->clientid;
    echo '<br>';
    echo $var;

    have also tried to echo $obj but not it will not

    Got it thank you very much

    $var = $arr->clientid;
    Last edited by techboy992; June 18th, 2013 at 08:02 AM. Reason: solution found

IMN logo majestic logo threadwatch logo seochat tools logo