在WordPress用户订阅(Membermouse)PHP和API后通过通知脚本添加Mailchimp用户



我正在开发一个MemberMouse订阅Wordpress网站。用户通过网络表单注册会员资格后,我想调用一个脚本,该脚本将用户添加到 Mailchimp 邮件列表中,并具有双重选择加入。Membermouse通过Mailchimp提供集成,但只有一次选择加入。因此,根据政府法律,我需要使用双重选择加入。

我写了以下脚本,应该在添加成员后发送以下 php sript 的条件下调用该脚本:

<?php
require_once("wp-load.php");
require_once("wp-content/plugins/membermouse/includes/mm-constants.php");
require_once("wp-content/plugins/membermouse/includes/init.php");

// Your Membermouse API URL
$apiUrl = "MYDOMAIN/wp-content/plugins/membermouse/api/request.php";
// Your API key
$apiKey = "my API key";
// Your API secret
$apiSecret = "my API secret";

// ---- GET EVENT TYPE ----
if(!isset($_GET["mm_member_add"]))
{
// event type was not found, so exit
exit;
}

// ---- ACCESS DATA ----
// member data
$username = $_GET["username"]; 
$email = $_GET["email"];

$apiKey1 = 'my Mailchimp API key';
$listID1 = 'my Mailchimp list ID';
// MailChimp API URL
$memberID1 = md5(strtolower($email));
$dataCenter1 = substr($apiKey1,strpos($apiKey1,'-')+1);
$url1 = 'https://' . $dataCenter1 . '.api.mailchimp.com/3.0/lists/' . $listID1 . '/members/' . $memberID1;
// member information
$json = json_encode([
'email_address' => $email,
'status'        => 'pending',               
]);
// send a HTTP POST request with curl
$ch1 = curl_init($url1);
curl_setopt($ch1, CURLOPT_USERPWD, 'user:' . $apiKey1);
curl_setopt($ch1, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch1, CURLOPT_TIMEOUT, 10);
curl_setopt($ch1, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch1, CURLOPT_POSTFIELDS, $json);
$result1 = curl_exec($ch1);
$httpCode = curl_getinfo($ch1, CURLINFO_HTTP_CODE);
curl_close($ch1);
break;
echo "<pre>".print_r($result1, true)."</pre>";
?>

但是,它不会响应。脚本将被执行。

目前为止,一切都好。但是,几天以来我一直在尝试如何编写此 PHP 以使其正常工作。此外,我不确定PHP代码是否正确。这只是一个假设。

有关参考,我从membermouse中找到了此脚本: https://dl.dropboxusercontent.com/u/265387542/files/member_notification_script.php

还有这个为Mailchimp编写的脚本:https://www.codexworld.com/add-subscriber-to-list-mailchimp-api-php/

我试图将这两者结合起来,但到目前为止没有成功。

感谢您的快速回复@scottcwilson,但是我没有得到来自此php文件的回复。

这是它现在的样子:

<?php
require_once("wp-load.php");
require_once("wp-content/plugins/membermouse/includes/mm-constants.php");
require_once("wp-content/plugins/membermouse/includes/init.php");

// Your API URL
$apiUrl = "MYDOMAIN/wp-content/plugins/membermouse/api/request.php";
// Your API key
$apiKey = "My API key";
// Your API secret
$apiSecret = "My API secret";

// ---- GET EVENT TYPE ----
if(!isset($_GET["mm_member_add"]))
{
// event type was not found, so exit
exit;
}

// ---- ACCESS DATA ----
// member data
$username = $_GET["username"];
$email = $_GET["email"];

$apiKey1 = 'Mailchimp API';
$listID1 = 'Mailchimp List ID';
// MailChimp API URL
$memberID1 = md5(strtolower($email));
$dataCenter1 = substr($apiKey1,strpos($apiKey1,'-')+1);
$url1 = 'https://' . $dataCenter1 . '.api.mailchimp.com/3.0/lists/' . $listID1 . '/members/';
// member information
$json = json_encode([
'email_address' => $email,
'status'        => 'pending',               
]);
// send a HTTP POST request with curl
$ch1 = curl_init($url1);
curl_setopt($ch1, CURLOPT_USERPWD, 'api_v3:' . $apiKey1);
curl_setopt($ch1, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch1, CURLOPT_TIMEOUT, 10);
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch1, CURLOPT_POSTFIELDS, $json);
$result1 = curl_exec($ch1);
$httpCode = curl_getinfo($ch1, CURLINFO_HTTP_CODE);
curl_close($ch1);
break;
echo "<pre>".print_r($result1, true)."</pre>";
?>

你说的没错,对吧?

当前代码的问题包括:

1( 您在 URL 上提供成员 ID - 这不是您希望 POST 创建新用户的方式。 所以做

$url1 = 'https://' . $dataCenter1 . '.api.mailchimp.com/3.0/lists/' . $listID1 . '/members/';

相反。

2(您的某些cURL设置是错误的。 对于您想要的 POST 字段

curl_setopt($ch1, CURLOPT_USERPWD, 'api_v3:' . $apiKey1);
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, true);

摆脱

curl_setopt($ch1, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, false);

最新更新