如何从 MailChimp API v3.0 批量操作返回错误

Sta*_*ace 2 php mailchimp mailchimp-api-v3.0

我\xe2\x80\x99m 正在努力解决新的 MailChimp API 和批处理功能,特别是如何从批处理的基础操作(而不是批处理操作本身)返回任何错误。

\n\n

我的代码如下,用于添加两个测试订阅者。响应仅显示整个批次的成功:

\n\n
[errored_operations] => 0\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果我再次运行它,它将返回类似的响应,但有两个错误:

\n\n
[errored_operations] => 2\n
Run Code Online (Sandbox Code Playgroud)\n\n

除此之外,没有任何迹象表明失败的原因或失败的原因。在本例中,我们知道 it\xe2\x80\x99s,因为用户已经订阅了。如果我尝试在不使用批量调用的情况下添加单个用户POST /lists/{list_id}/members,我会收到一条响应,其中详细说明了失败的原因。

\n\n
stdClass Object\n(\n    [type] => http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/\n    [title] => Member Exists\n    [status] => 400\n    [detail] => mary@jackson.net is already a list member. Use PUT to insert or update list members.\n    [instance] => \n)\n
Run Code Online (Sandbox Code Playgroud)\n\n

添加(或更新或删除)数百个订阅者时如何捕获单个错误?

\n\n

我尝试过循环访问用户,进行多个单独的调用,这很有效:它添加了用户和/或提供了详细的错误报告。但是,当 API 设置为在单次调用中处理此问题时,进行 500 次调用似乎很愚蠢。感谢您的任何想法!

\n\n

这是我的代码:

\n\n
$list_id = 'xyz123';\n$subscribers = array(\n    array(\n        'email'     => 'jeff@jackson.net',\n        'status'    => 'subscribed',\n        'firstname' => 'Jeff',\n        'lastname'  => 'Jackson'\n    ),\n    array(\n        'email'     => 'mary@jackson.net',\n        'status'    => 'subscribed',\n        'firstname' => 'Mary',\n        'lastname'  => 'Jackson'\n    )\n);\n\n$add_subs_batch = add_subs_batch($list_id, $subscribers);\necho '<pre>add_subs_batch: ';\nprint_r($add_subs_batch);\necho '</pre>';\n\nfunction add_subs_batch($list_id, $data) {\n    $method = 'POST'; \n    $batch_path = 'lists/' . $list_id . '/members';\n    $result = mc_request_batch($method, $batch_path, $data);\n    if($result && $result->id) {\n        $batch_id = $result->id;\n        $batch_status = get_batch_status($batch_id);\n        return $batch_status;\n    }\n    else {\n        return $result;\n    }\n}\nfunction get_batch_status($batch_id, $i=1) {\n    $method = 'GET'; \n    $target = 'batches/'.$batch_id;\n    $result = mc_request($method, $target, $data);\n    sleep(1); // wait 1 second and try\n    if($result->status == 'finished' ) {\n        return $result;\n    }\n    else {\n        return get_batch_status($batch_id, $i+1);\n    }\n}\nfunction mc_request_batch( $method, $batch_path, $data = false ) {\n    $api_key = '12345-us1';\n    $dataCenter = substr($api_key,strpos($api_key,'-')+1);\n    $url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/';\n    $target = 'batches';\n\n    $ch = curl_init();\n    curl_setopt($ch, CURLOPT_URL, $url . $target );\n    curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $api_key);\n    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);\n    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST' );\n    curl_setopt($ch, CURLOPT_TIMEOUT, 10 );\n    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );\n    curl_setopt($ch, CURLOPT_USERAGENT, 'YOUR-USER-AGENT' );\n    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); \n\n    if( $data ) {\n        $batch_data = new stdClass();\n        $batch_data->operations = array();\n            foreach ($data as $item) {\n                $batch = new stdClass();\n                $batch->method = $method;\n                $batch->path = $batch_path;\n                $batch->body = json_encode( array(\n                    'email_address' => $item['email'],\n                    'status'        => $item['status'],\n                    'merge_fields'  => array( \n                        'FNAME' => $item['firstname'],\n                        'LNAME' => $item['lastname']\n                    )\n                ) );\n                $batch_data->operations[] = $batch;\n            }\n\n        $batch_data = json_encode($batch_data);\n        curl_setopt($ch, CURLOPT_POSTFIELDS,  $batch_data  );\n        $response = curl_exec( $ch );\n    }\n    curl_close( $ch );\n    return json_decode($response);\n}\n
Run Code Online (Sandbox Code Playgroud)\n

Mûh*_*r K 5

您将收到id批处理操作的响应。这是“批次 ID”,它是唯一标识批次请求的字符串。

要获取批量请求的状态,您必须对 URL 调用 GET 请求 /batches/{batch_id}

从响应中,您可以在response_body_url字段中找到一个 URL,其中包含批处理调用中所有操作结果的 gzip 压缩存档。

参考:

笔记

出于安全原因,response_body_url 的有效期仅为 10 分钟。10 分钟后,通过 GET 调用 /3.0/batches/{batch_id} 生成另一个。

发出批量操作请求后,结果在 7 天内可用。