无法让 MailChimp 使用 API 和 Curl 保存我的数据

2024-03-21

我已经尝试过使用curl将数据发送到MailChimp但无法获取 要保存在 MailChimp 中的数据。任何对此的帮助将不胜感激!

这是我的代码:

$mailChimpUrl = "http://us2.api.mailchimp.com/1.3/?method=listSubscribe";
$merges = array(
    'FNAME'=>'Dave', 
    'LNAME'=>'Gilmour',
    'BUILDING'=>'Central High School',
    'MMERGE17' => '35904',
    'MMERGE12'=>'Yes'
);

$apikey="myrealapiishere-us2";
$listId="myrealformidishere";
$email="[email protected] /cdn-cgi/l/email-protection";
$double_optin=true;
$update_existing=false;
$replace_interests=true;
$send_welcome=false;
$email_type = 'html';            
$data = array(
    'email_address'=>$email,
    'apikey'=>$apikey,
    'merge_vars' => $merges,
    'id' => $listId,
    'double_optin' => $double_optin,
    'update_existing' => $update_existing,
    'replace_interests' => $replace_interests,
    'send_welcome' => $send_welcome,
    'email_type' => $email_type
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $mailChimpUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));      
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$result = curl_exec($ch);
curl_close($ch);

这是简单的 MailChimp PHP API 3.0 Curl 示例代码片段

<?PHP

// put your api key here  note the ending text past the - this is your datacenter 
// the datacenter needs to be added into to the url in the curlopt_url (see below)
$apikey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-us11"; // my datacenter was "us11"


// listid goes here - to find this... log into mail chimp go to Lists menu , 
// look to far right of list name for a drop down arrow, select the "Settings" dropdown,
// scroll to bottom and look  for  "Unique id for list"
$list_id = "xxxxxxxxxx"; // web site list


// the data I used to register (there may be others you can use, check API docs)
$email = "<<email_address_to_register>>";
$fname = "<<first_name>>";
$lname = "<<last_name>>";


$auth = base64_encode( 'user:'.$apikey );


// Notice the value of 'status' is 'pending'  
// I found this via a google search indicating a double opt in subscription process 

$data = array(
'apikey'        => $apikey,
'email_address' => $email,
'status'        => 'pending',
'merge_fields'  => array(
'FNAME' => $fname,
'LNAME' => $lname,
)
);
$json_data = json_encode($data);

$ch = curl_init();

// notice datacenter  "us11" comes after the // - make sure you update this to your datacenter (e.g. us2, us7 etc) or you'll get the "wrong datacenter" error.
$curlopt_url = "https://us11.api.mailchimp.com/3.0/lists/$list_id/members/";
curl_setopt($ch, CURLOPT_URL, $curlopt_url);

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
    'Authorization: Basic '.$auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/3.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);

$result = curl_exec($ch);
/*
// some debug statements 
var_dump($result);
print_r ($result);
*/


// here is simple way to determine status of a subscription
// $result is in JSON format
// this following loop is a simple JSON decode loop I found via google


 $status = "undefined";
    $msg = "unknown error occurred";
$myArray = json_decode($result, true);

foreach($myArray as $key => $value)
{

    // debug key<<< = >>>$value<<< <br>";

    if( $key == "status" )
    {
        $status=$value;
        //debug                 echo" status found $status<Br>";
    }
    else if ($key == "title")
    {
        $msg=$value;
        //debug                 echo" title found $msg<Br>";
    }


}

// create the output that gets displayed or returned if invoked by AJAX method
if( $status == "pending" )
{
    $msg = "Success! <br>$email has been subscribed <Br>check your inbox for the confirmation email to complete your subscription";
}
else
{
    $msg = "Sorry can not subscribe email $email <br>$msg <Br>";
}


echo "$msg <br>";


die(' '); // frees up mem etc..

?>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法让 MailChimp 使用 API 和 Curl 保存我的数据 的相关文章

  • 使用 php 执行 *.sql 文件

    我需要执行一个 sql 文件 其中大约有 48 个要创建的表 它由注释和以 结尾的sql命令组成 有没有办法运行这些 sql 命令 将它们立即转换为单个字符串 我的意思是我需要使用 php 立即运行整个文件 我可以使用 mysql quer
  • Laravel 输入:get() 不起作用

    我正在尝试在 Laravel 4 中使用 post 函数 我的表单有许多字段 在提交时 会转到控制器中的 post 函数 问题是 表单中的字段名称之间有空格 例如 Type 1 是输入的名称 中间有一个空格 现在 当我尝试通过执行以下操作来
  • 查找字符串中只出现一次的字符

    我正在用 PHP 编写一个算法来解决给定的数独难题 我已经设置了一个带有两个类的面向对象的实现 Square9x9 棋盘上每个单独图块的类 以及Sudoku类 其矩阵为Squares 代表董事会 我正在使用的算法的实现是一种三层方法 第一步
  • Selinux 阻止来自 php 的 crontab 命令

    我们的服务器上有 Fedora 25 和 apache 我想要这样做 以便我们网站上的 php 脚本可以更改 crontab 设置 我创建了以下测试 php 脚本
  • php ftp 检查文件夹是否存在总是返回创建文件夹错误

    有人可以告诉我这段代码中做错了什么吗 if id if is dir public html tem pasta path pics id echo pasta j existia destination file public html
  • 根据自定义数组位置排序帖子

    我想根据自定义字段列出帖子列表 这里我有 9 个帖子 有不同的 3 个位置 中 上 下 Post ID title position 1 Post1 Top 2 Post2 Bottom 3 Post3 Top 4 Post4 Bottom
  • 如何解决curl php中的HTTP/1.1 400 Bad Request

    我必须打一个 aspx来自 php 代码的页面 url 我试图使用curl 来访问 但出现以下错误并且 url 中没有空格 HTTP 1 1 400 Bad Request Content Type text html charset us
  • 获取 UNIX 时间戳的正确小时

    我认为这是一个愚蠢的问题 但似乎我找不到答案 我有这个时间戳 1295598602 在我的 php 脚本中我有 date date 年月日 1295598602 小时 日期 H 1295598602 00 这将返回 日期 2011 01 2
  • 无法加载请求的类:会话

    我的配置文件看起来像这样 gt config sess cookie name ci session config sess expiration 7200 config sess expire on close TRUE config s
  • Symfony 4 和 Doctrine 2 从集合中删除(第一个)项目后序列化导致转换为 JSON 对象而不是数组

    我在序列化已删除第一个元素的集合时遇到很多麻烦 我有 CompaniesCollection 实体 与 Company 实体有 Many2Many 关系 ORM ManyToMany targetEntity App Entity Comp
  • 将base64转换为base62(不含特殊字符)

    我想在 URL 中传递河豚加密字符串 并希望像 base64 一样对其进行编码 但没有任何特殊字符 像 base62 这样的东西就很好 0 9a zA Z 所以我想做的是使用base64 encode 转换河豚加密字符串 并将base64
  • 通过 CloudFlare 获取正确的访客 IP [重复]

    这个问题在这里已经有答案了 我正在使用其他人转售给我的 cPanel 这可能意味着我无法使用mod cloudflare 我想获取访问者的 IP 而不是 CloudFlare IP 我正在使用的代码部分 SERVER REMOTE ADDR
  • 在 Magento 中添加自定义折扣订单总计不会更改销售税

    我创建了一个自定义订单总额 在某些情况下会提供折扣 总计总是正确的 但是销售税计算在计算时没有考虑我的折扣 因此 如果我提供 10 美元的折扣 则销售税金额是根据折扣前的全部金额计算的 以下面为例 Subtotal 856 49 Multi
  • 课程完成时更新外部数据库

    我的场景 Moodle 中的用户完成了一门课程 一旦发生这种情况 我想更新外部数据库 我的理解是 每次运行 cron 作业时都会触发 course completed 事件 使用一些简单值 例如已完成课程的用户名 ID 课程 ID 以及完成
  • php 如何使用 getimagesize() 检查上传时的图像类型[重复]

    这个问题在这里已经有答案了 可能的重复 GetImageSize 在应该返回 FALSE 时没有返回 FALSE https stackoverflow com questions 10464948 getimagesize not ret
  • 动态创建多个上传文件

    我想知道是否有人知道动态创建上传表单的最佳方法 这就是我想要实现的目标 下面显示的代码允许一次上传 我想要一个按钮 按下该按钮后 应添加另一种形式用于文件上传 因此 如果我想上传 假设有 7 个文件 我想按按钮 7 次来创建这些上传表单 每
  • Ionic框架和php mysql

    我是 Ionic Apahce Cordova 的新手 我创建了一个简单的应用程序 它具有静态列表视图项 但我想从 MYSQL 表获取数据并将其替换到我的静态列表中 我用谷歌搜索了一下 有人在研究它 但我不知道应该把我的 php 文件放在哪
  • 运行 php 脚本的 Bash 脚本

    我有一个 php 脚本 我想使用 bash 脚本运行它 所以我可以使用 Cron 每分钟左右运行 php 脚本 据我所知 我需要创建 bash 脚本来处理 php 脚本 然后我才能使用 Cron 工具 计时器 到目前为止 我被告知我需要输入
  • mysql_insert_id 带更新

    执行下面的查询后 我使用 PHP 函数mysql insert id 它总是给我0 UPDATE tbl training types SET fld serial serial no fld name training name fld
  • 按某些字段排序的迭代学说集合

    我需要这样的东西 products Products getTable gt find 274 foreach products gt Categories gt orderBy title as category echo categor

随机推荐