gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:04:35 24 4
gpt4 key购买 nike

我正在努力使用新的 MailChimp API 和批处理功能,特别是如何从批处理的底层操作(而不是批处理操作本身)返回任何错误。

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

[errored_operations] => 0

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

[errored_operations] => 2

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

stdClass Object
(
[type] => http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/
[title] => Member Exists
[status] => 400
[detail] => <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="224f43505b6248434149514d4c0c4c4756" rel="noreferrer noopener nofollow">[email protected]</a> is already a list member. Use PUT to insert or update list members.
[instance] =>
)

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

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

这是我的代码:

$list_id = 'xyz123';
$subscribers = array(
array(
'email' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b6dcd3d0d0f6dcd7d5ddc5d9d898d8d3c2" rel="noreferrer noopener nofollow">[email protected]</a>',
'status' => 'subscribed',
'firstname' => 'Jeff',
'lastname' => 'Jackson'
),
array(
'email' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f59894878cb59f94969e869a9bdb9b9081" rel="noreferrer noopener nofollow">[email protected]</a>',
'status' => 'subscribed',
'firstname' => 'Mary',
'lastname' => 'Jackson'
)
);

$add_subs_batch = add_subs_batch($list_id, $subscribers);
echo '<pre>add_subs_batch: ';
print_r($add_subs_batch);
echo '</pre>';

function add_subs_batch($list_id, $data) {
$method = 'POST';
$batch_path = 'lists/' . $list_id . '/members';
$result = mc_request_batch($method, $batch_path, $data);
if($result && $result->id) {
$batch_id = $result->id;
$batch_status = get_batch_status($batch_id);
return $batch_status;
}
else {
return $result;
}
}
function get_batch_status($batch_id, $i=1) {
$method = 'GET';
$target = 'batches/'.$batch_id;
$result = mc_request($method, $target, $data);
sleep(1); // wait 1 second and try
if($result->status == 'finished' ) {
return $result;
}
else {
return get_batch_status($batch_id, $i+1);
}
}
function mc_request_batch( $method, $batch_path, $data = false ) {
$api_key = '12345-us1';
$dataCenter = substr($api_key,strpos($api_key,'-')+1);
$url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/';
$target = 'batches';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . $target );
curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $api_key);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST' );
curl_setopt($ch, CURLOPT_TIMEOUT, 10 );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_USERAGENT, 'YOUR-USER-AGENT' );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

if( $data ) {
$batch_data = new stdClass();
$batch_data->operations = array();
foreach ($data as $item) {
$batch = new stdClass();
$batch->method = $method;
$batch->path = $batch_path;
$batch->body = json_encode( array(
'email_address' => $item['email'],
'status' => $item['status'],
'merge_fields' => array(
'FNAME' => $item['firstname'],
'LNAME' => $item['lastname']
)
) );
$batch_data->operations[] = $batch;
}

$batch_data = json_encode($batch_data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $batch_data );
$response = curl_exec( $ch );
}
curl_close( $ch );
return json_decode($response);
}

最佳答案

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

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

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

引用:

Note

For security reasons, response_body_url is only valid for 10 minutes. After 10 minutes, generate another with a GET call to /3.0/batches/{batch_id}.

After you make the batch operation request, results are available for 7 days.

关于php - 如何从 MailChimp API v3.0 批量操作返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40137674/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com