skip to Main Content

When you retrieve Facebook Leads using the “leadgen_id” (that you got from the Webhook payload) from Graph-API, you get a json object that looks like this:

{
    "created_time": "2019-10-28T03:02:49+0000",
    "id": "2505284979588949",
    "field_data": [
      {
        "name": "groupID",
        "values": [
          "a3da23rad6"
        ]
      },
      {
        "name": "email",
        "values": [
          "[email protected]"
        ]
      },
      {
        "name": "first_name",
        "values": [
          "Edward"
        ]
      }
    ]
  }

You can use the php function json_decode($json, true) to convert the json to an array and you get a multidimensional array that looks like this:

array(3) {
  ["created_time"]=>
  string(24) "2019-10-28T03:02:49+0000"
  ["id"]=>
  string(16) "2505284979588949"
  ["field_data"]=>
  array(3) {
    [0]=>
    array(2) {
      ["name"]=>
      string(7) "groupID"
      ["values"]=>
      array(1) {
        [0]=>
        string(10) "a3da23rad6"
      }
    }
    [1]=>
    array(2) {
      ["name"]=>
      string(5) "email"
      ["values"]=>
      array(1) {
        [0]=>
        string(22) "[email protected]"
      }
    }
    [2]=>
    array(2) {
      ["name"]=>
      string(10) "first_name"
      ["values"]=>
      array(1) {
        [0]=>
        string(6) "Edward"
      }
    }
  }
}

Note: In my example, I have a key:value called ‘groupID’, this is a “Tracking Parameter” set in the Facebook forms setup. In this case it refers to a Mailchimp interest group ID.

Okay, cool. All you need to do to get the data is refer to the exact array key and viola you have your data.

echo $data['field_data'][2]['values'][0];

// Edward

There’s a problem.

The data you get needs to be in the exact order every time. If Facebook sends you the name before the email, your code doesn’t work.

I ran into this problem with all of my lead ads, for some reason, even though I setup every Lead Form the exact same order, Facebook will randomly rearrange the data causing errors.

My solution (not necessarily the right solution):

Most of the multidimensional array is the same, the only thing that changes is the second column key and values in that column.

$data['field_data'][0]
$data['field_data'][1]
$data['field_data'][2]

The 3rd column of the array with the “name” key contains the contents of that block.
The data that we need is in the first key of the array (4th column) of the “value” key.

$data['field_data'][2]['name']; //first_name
$data['field_data'][2]['value'][0]; //Edward

So, I just use a for loop and a switch statement to loop through the second column keys and assign the proper values.

for($i = 0; $i <= 2; $i++){

    switch ($data['field_data'][$i]['name']){
        case 'groupID':
            echo "Group ID = " . $data['field_data'][$i]['values'][0];
            break;
        case 'email':
            echo "Email = " . $data['field_data'][$i]['values'][0];
            break;
        case 'first_name':
            echo "First Name = " . $data['field_data'][$i]['values'][0];
            break;
          }
  }

//Group ID = a3da23rad6
//Email = [email protected]
//First Name = Edward

It’s not the most elegant solution, but it works for me.

My question: Is there a better way to handle the data in php?

I’ve tried nested loops and recursion, but I found my solution the simplest.

Reference: Facebook Lead Ad Retrieval

2

Answers


  1. I would do something like this for sanity:

    $parsed = array();
    foreach ($data['field_data'] as $item) {
        if (!isset($parsed[$item['name']])) {
            $parsed[$item['name']] = $item['values'];
        } else {
            $parsed[$item['name']] = array_merge($parsed[$item['name']], $item['values']);
        }
    }
    
    

    At this point the $parsed map should contain all of the values for each key name. I added an array merge just in case there are duplicate keys.

    echo "Group ID = {$parsed["groupID"]}";
    echo "Email = {$parsed["email"]}";
    echo "First Name = {$parsed["first_name"]}";
    
    Login or Signup to reply.
  2. You could use array_reduce (or a loop) to grab the key/value pairs:

    $fields = array_reduce($data['field_data'], static function ($fields, $entry) {
      $fields[$entry['name']] = $entry['values'];
      return $fields;
    });
    

    Note that this supposes every field is unique, though it can easily be adjusted if they’re not.

    You can also do $fields[$entry['name']] = reset($entry['values']) ?: null; to retrieve a single value every time, though the format (array) seems to imply there might be several.

    Demo: https://3v4l.org/kTl1M

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search