来自HTTP响应的PHP Parse Json& Foreach迭代数组

时间:2015-12-03 17:21:44

标签: php json sendgrid webhooks infusionsoft

关于这个主题有几个有针对性的问题,但在尝试了所有这些问题后,我觉得有必要寻求帮助。我正在使用Sendgrid,他们的webhook发送了一个json响应,由于某种原因,我一直无法弄清楚如何与我们的CRM集成。

更新:2015年12月3日@ 1:01 pm - 我有一些代码可以使用(可能不是最有效但可行的)见下文。

发送Sendgrid的示例JSON:

    [
  {
    "sg_message_id":"sendgrid_internal_message_id",
    "email": "john.doe@sendgrid.com",
    "timestamp": 1337197600,
    "smtp-id": "<4FB4041F.6080505@sendgrid.com>",
    "event": "spamreport"
  },
  {
    "sg_message_id":"sendgrid_internal_message_id",
    "email": "john.doe@sendgrid.com",
    "timestamp": 1337966815,
    "category": "newuser",
    "event": "bounce",
    "url": "https://sendgrid.com"
  },
  {
    "sg_message_id":"sendgrid_internal_message_id",
    "email": "john.doe@sendgrid.com",
    "timestamp": 1337969592,
    "smtp-id": "<20120525181309.C1A9B40405B3@Example-Mac.local>",
    "event": "unsubscribe",
    "asm_group_id": 42
  }
]

我遇到的问题是JSON中的每个数组都与特定事件有关。例如&#34; Spam&#34;或&#34; Bounced&#34;或&#34;取消订阅&#34;。我已将其设置为仅发送这三个事件。然而,有可能有人会先反弹,然后获得它,然后点击垃圾邮件,然后点击取消订阅。

对此最简单的解决方案(忽视层次系统)是将这些事件中的每一个传递到CRM中的特定字段。例如,如果事件=垃圾邮件,那么它将使用&#34;垃圾邮件&#34;填充$垃圾邮件。但是,如果它不存在,它应该什么也不做。

最后一点,我必须传递一个标题,以便Sendgrid停止发送垃圾邮件。此外,我不是编程员,也不是程序员,我在过去几个月里刚刚做了一些事情。

我的剧本不工作:

        <?php

    $data = file_get_contents("php://input");
    $events = json_decode($data, true);

    require_once('Infusionsoft/infusionsoft.php');
    $contact = new Infusionsoft_Contact();

    $custom = array(
    '_SGBounce',
    '_SGSpam',
    '_SGUnsub'
    );
    $contact->addCustomFields($custom);

if (is_array($events) || $events instanceof Traversable) {    
foreach ($events as $event) {
        $email = $event['email'];
        if($event['event'] === 'bounce') {
            $bounce = 'bounce';
        } elseif ($event['event'] === 'spamreport') {
            $spam = 'spam';
        } elseif ($event['event'] === 'unsubscribe') {
            $unsub = 'unsubscribe';
        } else {
            die echo header("HTTP/1.1 200 OK");
    }
        process_event($event);
    }}
    if (empty($email)) {
        die echo header("HTTP/1.1 200 OK");
    } else {
        $contact->Email = $email;
        $contact->_SGBounce = $bounce;
        $contact->_SGSpam = $spam;
        $contact->_SGUnsub = $unsub;
        $contact->save();
    }

    header("HTTP/1.1 200 OK");
    ?>

我还有一个我正在使用的脚本写入文件,只是为了测试并查看是否可以获取正确的事件来编写。但是我没有成功让它迭代过去的第一个数组。我尝试使用key =&gt;将foreach()嵌套在另一个foreach()中。价值解决方案贴在另一个答案在这里然而,这也是一个死胡同。

任何提示,帮助和指导......都将不胜感激。

更新:下面的工作代码(如果这有助于某人)

<?php

$data = file_get_contents("php://input");
$events = json_decode($data, true);

require_once('Infusionsoft/infusionsoft.php');
$contact = new Infusionsoft_Contact();

$custom = array(
'_SendGrid',
);
$contact->addCustomFields($custom);

$emails = array();
$em = '';

if (is_array($events) || $events instanceof Traversable) {
    foreach ($events as $event) {
    $email = $event['email'];
    $em = $email;
    if($event['event'] === 'unsubscribe') {
        $event = 'unsubscribe';
        $unsubscribe = 'unsubscribe';
    } elseif($event['event'] === 'spamreport') {
        $event = 'spam';
        $spam = 'spam';
    } elseif($event['event'] === 'bounce') {
        $event = 'bounce';
        $bounce = 'bounce';
    } else {
        continue;
    }
    $default = array(
        'bounce' => false,
        'spam' => false,
        'unsubscribe' => false
    );
    if(!is_null($event)) {
        if(array_key_exists($email, $emails)) {
            $entry = $emails[$email];
        } else {
            $entry = $default;
        }

        switch($event) {
            case "bounce":
                $entry['bounce'] = true;
                break;
            case "spam":
                $entry['spam'] = true;
                break;
            case "unsubscribe":
                $entry['unsubscribe'] = true;
                break;
        }
    }
}}
if($unsubscribe === 'unsubscribe'){
    $param = 'unsubscribe';
} elseif($spam === 'spam'){
    $param = 'spam';
} elseif($bounce === 'bounce'){
    $param = 'bounce';
} else {
    echo header("HTTP/1.1 200 OK");
}

$contact->Email = $em;
$contact->_SendGrid = $param;
$contact->save();

header("HTTP/1.1 200 OK");
?>

1 个答案:

答案 0 :(得分:0)

你能做什么,就像这样处理;

$emails = array();

foreach ($events as $event) {
    $email = $event['email'];
    if($event['event'] === 'bounce') {
        $event = 'bounce';
    } elseif ($event['event'] === 'spamreport') {
        $event = 'spam';
    } elseif ($event['event'] === 'unsubscribe') {
        $event = 'unsubscribe';
    } else {
        continue;
    }
    // Set defaults
    $default = array(
        'bounce' => false,
        'spam' => false,
        'unsubscribe' => false
    );
    if(!is_null($event)) {
        if(array_key_exists($email, $emails)) {
            $entry = $emails[$email];
        } else {
            $entry = $default;
        }

        switch($event) {
            case "bounce":
                $entry['bounce'] = true;
                break;
            case "spam":
                $entry['spam'] = true;
                break;
            case "unsubscribe":
                $entry['unsubscribe'] = true;
                break;
        }
    }
}

然后,您最终会得到一个电子邮件列表,这些电子邮件是他们自己的数组,其中包含键bouncespamunsubscribe的布尔值。您可以对该数组执行循环以保存数据。