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

时间:2017-08-05 16:30:48

标签: php wordpress mailchimp mailchimp-api-v3.0

我正在使用MemberMouse订阅Wordpress网站。用户通过webform注册成为会员后,我想调用一个脚本,将用户添加到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/

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

2 个答案:

答案 0 :(得分:1)

感谢快速回复@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 :(得分:0)

您当前代码的问题是:

1)您正在URL上提供memberid - 这不是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);