0

I am trying to parse some data out of the Hubspot API response. The response looks like this json_decoded:

stdClass Object(
[addedAt] => 1411052909604
[vid] => 24
[canonical-vid] => 24
[merged-vids] => Array
    (
    )

[portal-id] => XXXXX
[is-contact] => 1
[profile-token] => AO_T-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[profile-url] => https://app.hubspot.com/contacts/XXXXX/lists/public/contact/_AO_T-XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[properties] => stdClass Object
    (
        [lastname] => stdClass Object
            (
                [value] => testtt
            )

        [firstname] => stdClass Object
            (
                [value] => test
            )

        [lastmodifieddate] => stdClass Object
            (
                [value] => 1411052906670
            )

    )

[form-submissions] => Array
    (
        [0] => stdClass Object
            (
                [conversion-id] => 85d24dd2-9ee9-4d47-b8f3-3035acbd8f3b
                [timestamp] => 1411052834097
                [form-id] => fb16efd9-23cc-4511-889c-204fc8b41dba
                [portal-id] => 401824
                [page-url] => http://wbl-1.hs-sites.com/test
                [canonical-url] => http://wbl-1.hs-sites.com/test
                [content-type] => landing-page
                [page-title] => test
                [page-id] => 1570433242
                [title] => Default Form (Sample)
                [first-visit-url] => http://wbl-1.hs-sites.com/test
                [first-visit-timestamp] => 1411052722970
                [meta-data] => Array
                    (
                    )

            )

    )

[list-memberships] => Array
    (
    )

[identity-profiles] => Array
    (
        [0] => stdClass Object
            (
                [vid] => 24
                [identities] => Array
                    (
                        [0] => stdClass Object
                            (
                                [type] => EMAIL
                                [value] => test@user.com
                                [timestamp] => 1411052834097
                            )

                        [1] => stdClass Object
                            (
                                [type] => LEAD_GUID
                                [value] => 0b6acf21-6cee-4c7b-b664-e65c11ee2d8e
                                [timestamp] => 1411052834201
                            )

                    )

            )

    )

[merge-audits] => Array
    (
    )

)

I'm looking specifically to try to dig out an email inside the indentities-profile area.

I've tried to do the following:

echo $results->contacts[0]->identity-profiles;

But it just gives me a value of 0

Then I try to go further into the array by doing:

echo $results->contacts[0]->identity-profiles[0];

But at that point - I get a parse error:

Parse error: syntax error, unexpected '['

What am I doing wrong? And how can I dig all the way down to identity-profiles[0]->identities->[0]->value

which should equal: test@user.com

What am I missing?

MrTechie
  • 1,797
  • 4
  • 20
  • 36

1 Answers1

3

As mentioned in the comment I would suggest to decode the JSON to an associative array by passing true as second parameter to json_decode. Example: json_decode($data, true) Than you could access your identity profiles by:

$results['contacts'][0]['identitiy-profiles']

If you still want to get the results as an object you have to access the properties the following way because they contain a -:

$results->contacts[0]->{'identity-profiles'}

sebbo
  • 2,929
  • 2
  • 20
  • 37
  • Ah - makes sense. I can can dig down into it now.print_r($results['contacts'][0]['identity-profiles'][0]['identities'][0]['value']); gives me the results I'm looking for. Thanks! – MrTechie Sep 18 '14 at 16:18