简体   繁体   中英

How can I get the email address from the following JSON feed?

How can I get the email address from the feed provided by HubSpot below:

{
  "contacts": [
    {
      "addedAt": 1390574181854,
      "vid": 204727,
      "canonical-vid": 204727,
      "merged-vids": [

      ],
      "portal-id": 62515,
      "is-contact": true,
      "profile-token": "AO_T-mMusl38dq-ff-Lms9BvB5nWgFb7sFrDU98e-3CBdnB7G2qCt1pMEHC9zmqSfOkeq2on6Dz72P-iLoGjEXfLuWfvZRWBpkB-C9Enw6SZ-ZASg57snQun5f32ISDfLOiK7BYDL0l2",
      "profile-url": "https://app.hubspot.com/contacts/62515/lists/public/contact/_AO_T-mMusl38dq-ff-Lms9BvB5nWgFb7sFrDU98e-3CBdnB7G2qCt1pMEHC9zmqSfOkeq2on6Dz72P-iLoGjEXfLuWfvZRWBpkB-C9Enw6SZ-ZASg57snQun5f32ISDfLOiK7BYDL0l2/",
      "properties": {
        "firstname": {
          "value": "Bob"
        },
        "lastmodifieddate": {
          "value": "1483461406481"
        },
        "company": {
          "value": ""
        },
        "lastname": {
          "value": "Record"
        }
      },
      "form-submissions": [

      ],
      "identity-profiles": [
        {
          "vid": 204727,
          "saved-at-timestamp": 1476768116149,
          "deleted-changed-timestamp": 0,
          "identities": [
            {
              "type": "LEAD_GUID",
              "value": "f9d728f1-dff1-49b0-9caa-247dbdf5b8b7",
              "timestamp": 1390574181878
            },
            {
              "type": "EMAIL",
              "value": "mgnew-email@hubspot.com",
              "timestamp": 1476768116137
            }
          ]
        }
      ],
      "merge-audits": [

      ]
    },
    {
      "addedAt": 1392643921079,
      "vid": 207303,
      "canonical-vid": 207303,
      "merged-vids": [

      ],
      "portal-id": 62515,
      "is-contact": true,
      "profile-token": "AO_T-mPMwvuZG_QTNH28c_MbhSyNRuuTNw9I7zJAaMFjOqL9HKlH9uBteqHAiTRUWVAPTThuU-Fmy7IemUNUvdtYpLrsll6nw47qnu7ACiSHFR6qZP1tDVZFpxueESKiKUIIvRjGzt8P",
      "profile-url": "https://app.hubspot.com/contacts/62515/lists/public/contact/_AO_T-mPMwvuZG_QTNH28c_MbhSyNRuuTNw9I7zJAaMFjOqL9HKlH9uBteqHAiTRUWVAPTThuU-Fmy7IemUNUvdtYpLrsll6nw47qnu7ACiSHFR6qZP1tDVZFpxueESKiKUIIvRjGzt8P/",
      "properties": {
        "firstname": {
          "value": "Ff_FirstName_0"
        },
        "lastmodifieddate": {
          "value": "1479148429488"
        },
        "lastname": {
          "value": "Ff_LastName_0"
        }
      },
      "form-submissions": [

      ],
      "identity-profiles": [
        {
          "vid": 207303,
          "saved-at-timestamp": 1392643921090,
          "deleted-changed-timestamp": 0,
          "identities": [
            {
              "type": "EMAIL",
              "value": "email_0be34aebe5@abctest.com",
              "timestamp": 1392643921079
            },
            {
              "type": "LEAD_GUID",
              "value": "058378c6-9513-43e1-a13a-43a98d47aa22",
              "timestamp": 1392643921082
            }
          ]
        }
      ],
      "merge-audits": [

      ]
    }
  ],
  "has-more": true,
  "vid-offset": 207303
}

I am able to get the other fields such as first name and last name using the snipped below (I am using WordPress, so the exact code is replicated using: wp_remote_get($url, $args) ) and inside the foreach I have the following code:

foreach($y->contacts as $z) {
   echo "First Name: " . $z->properties->firstname->value . '<br>';
   echo "Last Name: " . $z->properties->lastname->value . '<br>'; 
}

Hi you can do it this way using json_decode with true as second parameter to return a associative array:

php:

<?php
$str = '{
  "contacts": [
    {
      "addedAt": 1390574181854,
      "vid": 204727,
      "canonical-vid": 204727,
      "merged-vids": [

      ],
      "portal-id": 62515,
      "is-contact": true,
      "profile-token": "AO_T-mMusl38dq-ff-Lms9BvB5nWgFb7sFrDU98e-3CBdnB7G2qCt1pMEHC9zmqSfOkeq2on6Dz72P-iLoGjEXfLuWfvZRWBpkB-C9Enw6SZ-ZASg57snQun5f32ISDfLOiK7BYDL0l2",
      "profile-url": "https://app.hubspot.com/contacts/62515/lists/public/contact/_AO_T-mMusl38dq-ff-Lms9BvB5nWgFb7sFrDU98e-3CBdnB7G2qCt1pMEHC9zmqSfOkeq2on6Dz72P-iLoGjEXfLuWfvZRWBpkB-C9Enw6SZ-ZASg57snQun5f32ISDfLOiK7BYDL0l2/",
      "properties": {
        "firstname": {
          "value": "Bob"
        },
        "lastmodifieddate": {
          "value": "1483461406481"
        },
        "company": {
          "value": ""
        },
        "lastname": {
          "value": "Record"
        }
      },
      "form-submissions": [

      ],
      "identity-profiles": [
        {
          "vid": 204727,
          "saved-at-timestamp": 1476768116149,
          "deleted-changed-timestamp": 0,
          "identities": [
            {
              "type": "LEAD_GUID",
              "value": "f9d728f1-dff1-49b0-9caa-247dbdf5b8b7",
              "timestamp": 1390574181878
            },
            {
              "type": "EMAIL",
              "value": "mgnew-email@hubspot.com",
              "timestamp": 1476768116137
            }
          ]
        }
      ],
      "merge-audits": [

      ]
    },
    {
      "addedAt": 1392643921079,
      "vid": 207303,
      "canonical-vid": 207303,
      "merged-vids": [

      ],
      "portal-id": 62515,
      "is-contact": true,
      "profile-token": "AO_T-mPMwvuZG_QTNH28c_MbhSyNRuuTNw9I7zJAaMFjOqL9HKlH9uBteqHAiTRUWVAPTThuU-Fmy7IemUNUvdtYpLrsll6nw47qnu7ACiSHFR6qZP1tDVZFpxueESKiKUIIvRjGzt8P",
      "profile-url": "https://app.hubspot.com/contacts/62515/lists/public/contact/_AO_T-mPMwvuZG_QTNH28c_MbhSyNRuuTNw9I7zJAaMFjOqL9HKlH9uBteqHAiTRUWVAPTThuU-Fmy7IemUNUvdtYpLrsll6nw47qnu7ACiSHFR6qZP1tDVZFpxueESKiKUIIvRjGzt8P/",
      "properties": {
        "firstname": {
          "value": "Ff_FirstName_0"
        },
        "lastmodifieddate": {
          "value": "1479148429488"
        },
        "lastname": {
          "value": "Ff_LastName_0"
        }
      },
      "form-submissions": [

      ],
      "identity-profiles": [
        {
          "vid": 207303,
          "saved-at-timestamp": 1392643921090,
          "deleted-changed-timestamp": 0,
          "identities": [
            {
              "type": "EMAIL",
              "value": "email_0be34aebe5@abctest.com",
              "timestamp": 1392643921079
            },
            {
              "type": "LEAD_GUID",
              "value": "058378c6-9513-43e1-a13a-43a98d47aa22",
              "timestamp": 1392643921082
            }
          ]
        }
      ],
      "merge-audits": [

      ]
    }
  ],
  "has-more": true,
  "vid-offset": 207303
}';

$arr_json = json_decode($str,true);

foreach($arr_json["contacts"] as $key=>$value){
foreach($value["identity-profiles"] as $key2 => $item){
    foreach($item["identities"] as $key3 => $sub){
       if($sub["type"] == "EMAIL"){
            echo "\n";
            echo  $sub["value"];
       }
    }
}
    //echo $value["value"];

};


?>

you can try the code here http://sandbox.onlinephpfunctions.com/

Hope it helps

Thank you everybody for shedding some light, I eventually managed to get to the answer and here is my rough attempt (it's based on WordPress's $response = wp_remote_get( $url, $args ); ):

if( !is_wp_error( $response ) ) {

    $response_body = json_decode($response['body']);    
    $decoded_response_body = json_decode( json_encode($response_body) );

    if( isset($decoded_response_body->contacts) ) {

        foreach( $decoded_response_body->contacts as $contact) {

            // get user details
            $fist_name = $contact->properties->firstname->value;
            $last_name = $contact->properties->lastname->value;

            $identities = $contact->{'identity-profiles'}[0]->identities;
            foreach ( $identities as $key => $value ){
                if ( strtolower($value->type) === 'email' ){
                    $contact_email = $value->value;
                }
            }

        }   

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM