1

关于这个主题有几个重点问题,但在尝试了所有之后,我觉得有必要寻求帮助。我正在使用 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 中的每个数组都与特定事件有关。例如“垃圾邮件”、“退回”或“退订”。我已将其设置为只发送这三个事件。然而,有可能有人会先反弹,然后得到它,然后点击垃圾邮件,然后点击退订。

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

最后一点,我必须传递一个标题,以便 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() 中,并在此处的另一个答案中发布了 key => value 解决方案。然而,这也是一个死胡同。

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

更新:下面的工作代码(以防这对某人有帮助)

<?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");
?>
4

1 回答 1

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。您可以对该数组进行循环以保存数据。

于 2015-12-03T18:16:34.567 回答