PHP 从 HTTP 响应和 Foreach 迭代数组中解析 Json

PHP Parse Json from HTTP response & Foreach Iterate Array

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

更新:12/3/2015 @ 1:01pm - 我有一些代码可以工作(可能不是最有效的但它有效)见下文。

发送来自 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 中的每个数组都与特定事件有关。例如 "Spam",或 "Bounced" 或 "Unsubscribe"。我已将其设置为仅发送这三个事件。然而,有可能有人会先退回,然后收到,然后点击垃圾邮件,然后点击退订。

最简单的解决方案(忽略层次结构系统)是将这些事件中的每一个传递到我们 CRM 中的特定字段。例如,如果事件 = 垃圾邮件,那么它将用 "spam" 填充 $spam。但是,如果它不存在,它应该什么也不做。

最后一点,我必须传递 header 以便 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");
    ?>

我还有一个正在使用的脚本,它写入一个文件,只是为了测试我是否可以写入正确的事件。但是我没能让它遍历第一个数组。我试过将 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");
?>

你可以做的,就是这样处理;

$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 具有布尔值。您可以对该数组进行循环以保存数据。