1

このトピックに関するいくつかの焦点を絞った質問がありますが、それらすべてを試した後、助けを求める必要があると感じました. 私はSendgridを使用しています.SendgridのWebhookはjson応答を送信しますが、何らかの理由でCRMと統合する方法がわかりません.

更新: 2015 年 12 月 3 日 @ 午後 1:01 - 動作するコードを取得しました (おそらく最も効率的ではありませんが、動作します)。以下を参照してください。

送信される 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 の各配列が特定のイベントに関連していることです。たとえば、「スパム」、「バウンス」、「登録解除」などです。これら 3 つのイベントのみが送信されるように設定しました。ただし、誰かが最初にバウンスし、次に取得し、次にスパムにヒットし、次に購読解除にヒットする可能性があります。

これに対する最も簡単な解決策は (階層システムを無視して)、これらの各イベントを CRM の特定のフィールドに渡すことです。たとえば、イベント = スパムの場合、$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() にネストしようとしました。しかし、それも行き止まりでした。

ヒント、ヘルプ、ガイダンス...大歓迎です。

更新:以下の作業コード(これが誰かを助ける場合)

<?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