0

I'm coding a login system using API. i'm stuck with the result that i got using the API. I got the result as normal string like this:

stdClass Object
(
    [success] => 1
    [message] => Array
        (
            [code] => 000
            [txt] => Operation successful
        )

    [data] => Array
        (
            [result] => OK
            [accountID] => 000000000
            [group] => demoCAC
            [currency] => USD
            [enable] => 1
            [read_only] => 0
            [name] => Bogus Demo
            [country] => ****
            [city] => 
            [address] => 
            [phone] => **
            [email] => ***@gmail.com
            [status] => test
            [regdate] => 07/02/2017 09:57
            [lastdate] => 07/02/2017 09:57
            [balance] => 0.00
            [credit] => 0.00
            [equity] => 10000.00
            [isDemo] => 1
            [tp_version] => **
            [last_name] => Demo
            [first_name] => Bogus
            [domain] => ***.com
            [compliance] => 
            [latestTrades] => Array
                (
                    [results] => Array
                        (
                        )

                    [totalResults] => 0
                )

            [owner] => 
        )
    )

So how can i convert it to an array or useable variable ? * This information printed by ( echo ), and i can't access $result -> data ->... is there a function that convert this string to array or json or something readable so i can reach to specific value? when i try : print_r(get_object_vars($result)); i get this error:

Warning: get_object_vars() expects parameter 1 to be object, string given in /home/**/public_html/crm/profile.php on line 20

Unix3r
  • 19
  • 4
  • 4
    How did you print that information? If you were using `var_dump` or `print_r` then your result isn't actually a string but already an object, and thus have access to its properties (eg. `$result->success` will be equal to the success value, `1` in this case) – ccKep Feb 08 '17 at 13:11
  • it seems to be a var_dump display .. you can check eval function http://php.net/manual/fr/function.eval.php to convert string to php code – Fky Feb 08 '17 at 13:12
  • 1
    Is this the string that your api returns? If it is, you should switch to a format that is easy to parse like json. – jeroen Feb 08 '17 at 13:12
  • provide us more code – iwaduarte Feb 08 '17 at 14:11
  • 1
    Also DO NOT USE EVAL! Fky's suggestion is profoundly dangerous and will only lead to a world of hurt. – GordonM Feb 08 '17 at 14:19
  • Show us the code you use to fetch and print this API output. – Marc Compte Feb 08 '17 at 14:27

1 Answers1

0

Unless Im missing something, your problem is a simple one.

looking at the guts of your var_dump, you should be able to access the index in your question like:

$result->data['email'];  // where 'email' is the key of the data.

or

$result->data['latestTrades']['results']; // nested example.

all the properties of a standard class are public, and so can be accessed directly. And in your example, all of them contain an array, or nested array so can be accessed by their key.

DevDonkey
  • 4,835
  • 2
  • 27
  • 41