创建新的Mailchimp列表时出现HTTP 400错误

时间:2018-08-20 14:05:36

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

我正在尝试通过mailchimp api创建一个新列表,我已经尝试了很多代码调整,但是一直收到错误消息:

    {"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title":"Invalid Resource","status":400,"detail":"The resource submitted could not be validated. For field-specific details, see the 'errors' array.","instance":"0174d737-b3d4-40a6-9cd4-e934ed8578a7","errors":[{"
field":"","message":"Schema describes object, NULL found instead"}]}

我的代码(api密钥有效):

$data = array( // the information for your new list--not all is required
        "name" => $name,
        "contact" => array (
            "company" => $company,
            "address1" => $address1
        ),
        "permission_reminder" => $permission_reminder,
        "use_archive_bar" => $archive_bars,
        "campaign_defaults" => array(
            "from_name" => $from_name,
            "from_email" => $from_email,
            "subject" => $subject,
            "language" => $language
        ),
        "notify_on_subscribe" => $notify_subs,
        "notify_on_unsubscribe" => $notify_unsubs,
        "email_type_option" => $type,
        "visibility" => $visibility
    );


    $ch = curl_init("https://$dataCenter.api.mailchimp.com/3.0/lists/");
    curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $result = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    print_r($result); // display API response

1 个答案:

答案 0 :(得分:0)

在您提供的代码中没有看到有效负载的任何JSON编码。您需要json_encode()的有效载荷($data)才能使MailChimp能够读取它。

赞: curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));