【问题标题】:How to return errors from MailChimp API v3.0 batch operation如何从 MailChimp API v3.0 批量操作返回错误
【发布时间】:2017-03-01 11:09:05
【问题描述】:

我正在努力使用新的 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] => mary@jackson.net is already a list member. Use PUT to insert or update list members.
    [instance] => 
)

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

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

这是我的代码:

$list_id = 'xyz123';
$subscribers = array(
    array(
        'email'     => 'jeff@jackson.net',
        'status'    => 'subscribed',
        'firstname' => 'Jeff',
        'lastname'  => 'Jackson'
    ),
    array(
        'email'     => 'mary@jackson.net',
        '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);
}

【问题讨论】:

  • FWIW,我的应用程序不需要 CURLOPT_USERAGENT 行。

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


【解决方案1】:

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

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

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

参考:

注意

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

在您提出批量操作请求后,结果可用于 7 天。

【讨论】:

  • 感谢您抽出宝贵时间回复。如何在我的代码中访问该 gzip 归档文件?还有其他地方我可以使用 PHP 访问这些数据吗?
  • 您可以将代码写入 1. 将文件下载到您的服务器 Ref 。 2.从下载的gzip文件Ref中读取信息。如果您有任何障碍,请告诉我们。谢谢。 :)
  • 再次感谢您的回复。我理解您概述的流程,尽管该文件实际上是一个 .tar.gz,它提出了另一层复杂性,并且我无法将文件下载到服务器。但整个事情似乎非常复杂,只是得到一些基本的错误报告。真的没有其他方法可以访问这些数据吗?
  • 我认为没有任何方法可以访问此数据,将检查它并让您知道是否有。 :)
  • 感谢您到目前为止的帮助。我觉得我快到了,但被困在最后一站。我可以将 file.tar.gz 文件下载到服务器,我可以使用 PharData 类和解压缩函数将其解压缩到 path/file.tar。从那里,我正在尝试 phar:extractTo('path/file') 但它抛出异常Extraction from phar "/path/file.tar" failed: Cannot extract ".", internal error。有什么想法吗?
猜你喜欢
  • 2015-12-18
  • 2016-07-03
  • 2016-03-20
  • 2020-09-29
  • 2018-10-25
  • 2016-05-02
  • 2022-06-21
  • 2019-01-20
  • 2017-07-07
相关资源
最近更新 更多