MailChimp API 在 V3.0 PHP Curl 中响应 404

Posted

技术标签:

【中文标题】MailChimp API 在 V3.0 PHP Curl 中响应 404【英文标题】:MailChimp API Responds 404 in V3.0 PHP Curl 【发布时间】:2018-11-30 08:53:06 【问题描述】:

我正在使用以下代码向 MailChimp 中的列表发起成员添加请求。

<?php

error_reporting(-1);
    ini_set('display_errors', 1);

$email = 'test@domain.com';
$first_name = 'name';
$last_name = 'last name';

$api_key = 'xx-us18'; // YOUR API KEY

// server name followed by a dot. 
// We use us13 because us13 is present in API KEY
$server = 'us18.'; 

$list_id = 'xx'; // YOUR LIST ID

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

$data = array(
    'apikey'        => $api_key,
    'email_address' => $email,
    'status'        => 'subscribed',
    'merge_fields'  => array(
        'FNAME' => $first_name,
        'LNAME'    => $last_name
        )    
    );
$json_data = json_encode($data);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://'.$server.'api.mailchimp.com/3.0/lists/'.$list_id.'/members');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
    'Authorization: Basic '.$auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.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);

echo $result;
?>

但是发生的事情是响应是 404。


type: "http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/",
title: "Resource Not Found",
status: 404,
detail: "The requested resource could not be found.",
instance: "78a35efa-ef43-471d-9e70-aecdc992d2e6"

有谁知道我做错了什么?出现此错误的原因可能是什么?

额外信息:API 密钥有效,如果我提供错误,我会收到另一个错误。 List-id 也是如此。 我正在使用 MAMP 在 localhost 上运行此代码。

【问题讨论】:

如果你在浏览器中访问http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/,它会告诉你为什么这不起作用 【参考方案1】:

问题是我使用了 web_id 而不是 list_id。 当我改变它时,问题就解决了。

【讨论】:

谢谢你——出于同样的原因,我一直在用头撞墙一段时间。

以上是关于MailChimp API 在 V3.0 PHP Curl 中响应 404的主要内容,如果未能解决你的问题,请参考以下文章