June 18th, 2013, 07:24 AM
-
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
June 18th, 2013, 07:30 AM
-
$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!
June 18th, 2013, 07:33 AM
-
Hi,
by the way, JSON is not XML. Those are two completely different languages/formats.
June 18th, 2013, 07:39 AM
-
So
$var = $result->result; will give responce success if is success
$var = $user_id->clientid; will give the user id??
June 18th, 2013, 07:55 AM
-
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!
June 18th, 2013, 07:56 AM
-
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($jsondata, true);
// that's your array
print_r($response);
// get user ID
$user_id = $response['clientid'];
print_r($user_id);
June 18th, 2013, 07:58 AM
-
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