Mailchimp api 3.0 错误:“架构描述了对象,而是找到了数组”是代码还是在 mailchimp 的末尾?

Posted

技术标签:

【中文标题】Mailchimp api 3.0 错误:“架构描述了对象,而是找到了数组”是代码还是在 mailchimp 的末尾?【英文标题】:Mailchimp api 3.0 error: "Schema describes object, array found instead" is the code or on mailchimp's end? 【发布时间】:2018-06-22 06:23:53 【问题描述】:

我需要使用 mailchimp 的 api 和数据库中的订阅者列表批量更新我的 mailchimp 列表中的订阅者。

我正在尝试在此处实施问题的解决方案: Mailchimp API 3.0 Batch Subscribe 但我得到了错误

"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title":"Invalid Resource","status":400,"detail":"提交的资源无法验证。具体字段的详细信息,请参见'errors' 数组。","instance":"8198a6e9-9a7c-4e00-8365-3a7062f047f3","errors":["field":"","message":"Schema 描述了对象,而是找到了数组" ]

我见过有类似问题的人,有时是 mailchimp 端的问题,有时是数据传递方式的问题。任何关于我是否需要修复代码或联系 mailchimp 的见解将不胜感激!

有问题的脚本:

<?php
$apikey  = 'myapi'; // Your Mailchimp ApiKey
$list_id = 'mylist'; // your List ID Where you want to add subscriber

$servername = 'myserver';
$username   = 'myun';
$password   = 'mypw';
$dbname     = 'mydb';
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) 
    die('Connection failed: ' . $conn->connect_error);


$sql       = 'SELECT * FROM emails Limit 2';
$result    = $conn->query($sql);
$finalData = array();
if ($result->num_rows > 0) 
    // output data of each row
    while ($row = $result->fetch_assoc()) 
        $individulData = array(
            'apikey'        => $apikey,
            'email_address' => $row['email'],
            'status'        => $row['status'],//subscribe,pending,unsubscribe
            'merge_fields'  => array(
                'FNAME' => $row['FNAME'],
                'LNAME' => $row['LNAME'],
                'BARCODE' => $row['BARCODE'],
                'SERVICE' => $row['SERVICE'],
                'PURCHASE' => $row['PURCHASE'],
            )
        );

        $json_individulData        = json_encode($individulData);
        $finalData['operations'][] =
            array(
                "method" => "PUT",
                "path"   => "/lists/$list_id/members/",
                "body"   => $json_individulData
            );
    


$api_response = batchSubscribe($finalData, $apikey);
print_r($api_response);
$conn->close();

/**
 * Mailchimp API- List Batch Subscribe added function
 *
 * @param array  $data   Passed you data as an array format.
 * @param string $apikey your mailchimp api key.
 *
 * @return mixed
 */
function batchSubscribe(array $data, $apikey)

    $auth          = base64_encode('user:' . $apikey);
    $json_postData = json_encode($data);
    $ch            = curl_init();
    $dataCenter    = substr($apikey, strpos($apikey, '-') + 1);
    $curlopt_url   = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/batches/';
    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_postData);


    $result = curl_exec($ch);
    return $result;


?>

【问题讨论】:

【参考方案1】:

我遇到了类似的错误。 希望它对未来的用户仍然有用。

主要问题是 mailchimp 文档。


$operations = [];
foreach ($customers as $customer)

    if ($customer->hasEmail())
    
        $operations['operations'][] = (object) [
            'method'        => 'post',
            'path'          => '/lists/' . $settings['list_id'] . '/members',
            'body' => json_encode([
                'email_address' => $customer->email,
                'status'        => 'subscribed',
                'merge_fields' => (object) [],
            ])
        ];
    


try

    $response = $mailchimp->batches->start($operations);
    print_r($response);

catch (\Exception $e)

    // var_dump($e->getMessage());
    print_r($e->getResponse()->getBody()->getContents());

【讨论】:

谢谢! $operations['operations'][] = (object) [ 这条线对我来说是一种节省。我从 mailchimp 复制了示例,但它不起作用。但是你的解决方案救了我,谢谢!【参考方案2】:

我遇到了类似的问题,消息是“架构描述了字符串,而是找到了布尔值”。 json_encode() 好像有特殊字符的问题。

我用这段代码解决了它,也许它对你有用: utf8_encode($row['FNAME']) 或者您可以尝试使用 (string)。 我希望这可以帮助某人。我使用 POST 而不是 PUT 示例:

   $stringData =  array(
    'apikey'        => $api_key,
    'email_address' => $row['email'],
    'status'        => 'subscribed',
    'merge_fields'  =>  array(
        'FNAME'     =>  utf8_encode($row['FNAME']),
        'LNAME'     =>  utf8_encode($row['LNAME'])
    )
   );

【讨论】:

【参考方案3】:

强制转换对象是不必要的。

从 Kevin 的代码中重构:

$operations = [];
foreach ($customers as $customer)

    if ($customer->hasEmail())
    
        $operations[] = [
            'method'        => 'post',
            'path'          => '/lists/' . $settings['list_id'] . '/members',
            'body' => json_encode([
                'email_address' => $customer->email,
                'status'        => 'subscribed',
                'merge_fields' => [],
            ])
        ];
    


try

    $response = $mailchimp->batches->start(['operations'=>$operations]);
    print_r($response);

catch (\Exception $e)

    // var_dump($e->getMessage());
    print_r($e->getResponse()->getBody()->getContents());

缺失位是操作列表必须由请求有效负载中的“操作”键拥有

【讨论】:

以上是关于Mailchimp api 3.0 错误:“架构描述了对象,而是找到了数组”是代码还是在 mailchimp 的末尾?的主要内容,如果未能解决你的问题,请参考以下文章

Mailchimp api 3.0错误:“Schema描述对象,找到数组”是代码还是在mailchimp的结尾?

Mailchimp api 3.0 错误:“架构描述了对象,而是找到了数组”是代码还是在 mailchimp 的末尾?

使用 RestSharp 向 Mailchimp API 3.0 查询帐户详细信息时出现“API 密钥丢失”错误

为啥在连接到 mailchimp API 3.0 时出现 401 错误

无法通过 Java Maleorang 的 MailChimp API 3.0 包装器获取数据 - 404 错误

访问 MailChimp API 3.0 (GET)