将mailchimp web hook帖子捕获到SQL数据库

时间:2016-02-22 21:55:29

标签: php wordpress mailchimp

我正在尝试捕获名为' wp_signup_test'的数据库中的MailChimp列表的订阅。在我的服务器上。数据库连接由WordPress管理。基于MailChimp提供的PHP模板https://apidocs.mailchimp.com/webhooks/downloads/#php我可以写入服务器上的日志文件。我试图将订阅信息作为订阅函数MailChimp提供的一部分插入我的数据库:

function subscribe($data){
wh_log($data['email'] . ' just subscribed!');


$mailchimp_signup = array(
        'email' => $wpdb->escape($data['email']),
        'first_name' => $wpdb->escape($data['merges']['FNAME']),
        'last_name' => $wpdb->escape($data['merges']['LNAME']),
        'phone' => $wpdb->escape($data['merges']['MMERGE4']),
        'title' => $wpdb->escape($data['merges']['MMERGE3']),
        'euid' => $wpdb->escape($data['id']),
        'leid' => $wpdb->escape($data['list_id'])
        );
$wpdb->insert(wp_signup_test, $mailchimp_signup);

}

我在日志中捕获了这个。

Array
(
    [key] => 238624368237658724365
    [type] => subscribe
    [fired_at] => 2016-02-22 19:19:16
    [data] => Array
       (
            [id] => bb46fb127e
            [email] => john.smith@gmail.com
            [email_type] => html
            [ip_opt] => 104.162.217.167
            [ip_signup] => 104.162.217.167
            [web_id] => 27977133
            [merges] => Array
                (
                    [FNAME] => john
                    [LNAME] => smith
                    [EMAIL] => john.smith@gmail.com
                    [MMERGE4] => 55555555
                    [MMERGE3] => Yes
                )

            [list_id] => dbf9543f8d
        )

)

我无法捕获数据库中的信息。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

我找到了解决方案。当我将它放在交换机上方的条件语句中时,我将插入发布到switch函数中的数据库代码。

if ( !isset($_GET['key']) ){
    wh_log('No security key specified, ignoring request'); 
} elseif ($_GET['key'] != $my_key) {
    wh_log('Security key specified, but not correct:');
    wh_log("\t".'Wanted: "'.$my_key.'", but received "'.$_GET['key'].'"');
} else {

        //process the request

    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        if ($_POST['type'] == 'subscribe') {
            $mailchimp_signup = array(
            'email' => $wpdb->escape($_POST['data']['email']),
            'first_name' => 'test',
            'last_name' =>'test',
            'phone' => 'test',
            'title' => 'test',
            'euid' => 'test',
            'leid' => 'test'
        );

$wpdb->insert(wp_signup_test, $mailchimp_signup);


        }
    }


    wh_log('Processing a "'.$_POST['type'].'" request...');
    switch($_POST['type']){
        case 'subscribe'  : subscribe($_POST['data']);   break;
        case 'unsubscribe': unsubscribe($_POST['data']); break;
        case 'cleaned'    : cleaned($_POST['data']);     break;
        case 'upemail'    : upemail($_POST['data']);     break;
        case 'profile'    : profile($_POST['data']);     break;
        default:
            wh_log('Request type "'.$_POST['type'].'" unknown, ignoring.');
    }
}
wh_log('Finished processing request.');